Python pycurl.HTTPPOST Examples
The following are 7
code examples of pycurl.HTTPPOST().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pycurl
, or try the search function
.
Example #1
Source File: client.py From pycopia with Apache License 2.0 | 6 votes |
def get_form_poster(self): """Initialze a Curl object for a single POST request. This sends a multipart/form-data, which allows you to upload files. Returns a tuple of initialized Curl and HTTPResponse objects. """ data = self._data.items() c = pycurl.Curl() resp = HTTPResponse(self._encoding) c.setopt(c.URL, str(self._url)) c.setopt(pycurl.HTTPPOST, data) c.setopt(c.WRITEFUNCTION, resp._body_callback) c.setopt(c.HEADERFUNCTION, resp._header_callback) self._set_common(c) return c, resp
Example #2
Source File: smgr_upload_image.py From contrail-server-manager with Apache License 2.0 | 6 votes |
def send_REST_request(ip, port, payload, file_name, kickstart='', kickseed=''): try: response = StringIO() headers = ["Content-Type:application/json"] url = "http://%s:%s/image/upload" %( ip, port) conn = pycurl.Curl() conn.setopt(pycurl.URL, url) conn.setopt(pycurl.POST, 1) payload["file"] = (pycurl.FORM_FILE, file_name) if kickstart: payload["kickstart"] = (pycurl.FORM_FILE, kickstart) if kickseed: payload["kickseed"] = (pycurl.FORM_FILE, kickseed) conn.setopt(pycurl.HTTPPOST, payload.items()) conn.setopt(pycurl.CUSTOMREQUEST, "PUT") conn.setopt(pycurl.WRITEFUNCTION, response.write) conn.perform() return response.getvalue() except: return None
Example #3
Source File: hub.py From robot with MIT License | 6 votes |
def upload_file(self, path): """ 上传文件 :param path: 文件路径 """ img_host = "http://dimg.vim-cn.com/" curl, buff = self.generate_curl(img_host) curl.setopt(pycurl.POST, 1) curl.setopt(pycurl.HTTPPOST, [('name', (pycurl.FORM_FILE, path)), ]) try: curl.perform() ret = buff.getvalue() curl.close() buff.close() except: logger.warn(u"上传图片错误", exc_info=True) return u"[图片获取失败]" return ret
Example #4
Source File: test_bagofrequests.py From pyaem with MIT License | 5 votes |
def test_upload_file(self): curl = pycurl.Curl() curl.setopt = MagicMock() curl.perform = MagicMock() curl.getinfo = MagicMock(return_value=200) curl.close = MagicMock() pycurl.Curl = MagicMock(return_value=curl) url = 'http://localhost:4502/.cqactions.html' params = {'foo1': 'bar1', 'foo2': 'bar2'} handlers = {200: self._handler_dummy} result = pyaem.bagofrequests.upload_file(url, params, handlers, file='/tmp/somefile') curl.setopt.assert_any_call(pycurl.POST, 1) curl.setopt.assert_any_call(pycurl.HTTPPOST, [('foo1', 'bar1'), ('foo2', 'bar2')]) curl.setopt.assert_any_call(pycurl.URL, 'http://localhost:4502/.cqactions.html') curl.setopt.assert_any_call(pycurl.FOLLOWLOCATION, 1) curl.setopt.assert_any_call(pycurl.FRESH_CONNECT, 1) # 6 calls including the one with pycurl.WRITEFUNCTION self.assertEqual(curl.setopt.call_count, 6) curl.perform.assert_called_once_with() curl.getinfo.assert_called_once_with(pycurl.HTTP_CODE) curl.close.assert_called_once_with() self.assertEqual(result.is_success(), True) self.assertEqual(result.message, 'some dummy message') self.assertEqual(result.response['request']['method'], 'post') self.assertEqual(result.response['request']['url'], 'http://localhost:4502/.cqactions.html') self.assertEqual(result.response['request']['params'], params)
Example #5
Source File: test_bagofrequests.py From pyaem with MIT License | 5 votes |
def test_upload_file_unexpected(self): curl = pycurl.Curl() curl.setopt = MagicMock() curl.perform = MagicMock() curl.getinfo = MagicMock(return_value=500) curl.close = MagicMock() pycurl.Curl = MagicMock(return_value=curl) url = 'http://localhost:4502/.cqactions.html' params = {'foo1': 'bar1', 'foo2': 'bar2'} handlers = {} try: pyaem.bagofrequests.upload_file(url, params, handlers, file='/tmp/somefile') self.fail('An exception should have been raised') except pyaem.PyAemException as exception: self.assertEqual(exception.code, 500) self.assertEqual(exception.message, 'Unexpected response\nhttp code: 500\nbody:\n') curl.setopt.assert_any_call(pycurl.POST, 1) curl.setopt.assert_any_call(pycurl.HTTPPOST, [('foo1', 'bar1'), ('foo2', 'bar2')]) curl.setopt.assert_any_call(pycurl.URL, 'http://localhost:4502/.cqactions.html') curl.setopt.assert_any_call(pycurl.FOLLOWLOCATION, 1) curl.setopt.assert_any_call(pycurl.FRESH_CONNECT, 1) # 6 calls including the one with pycurl.WRITEFUNCTION self.assertEqual(curl.setopt.call_count, 6) curl.perform.assert_called_once_with() curl.getinfo.assert_called_once_with(pycurl.HTTP_CODE) curl.close.assert_called_once_with()
Example #6
Source File: mycurl.py From QMusic with GNU Lesser General Public License v2.1 | 4 votes |
def upload(self, url, data, header=None, proxy_host=None, proxy_port=None, cookie_file=None): ''' open url with upload @param url: the url to visit @param data: the data to upload @param header: the http header @param proxy_host: the proxy host name @param proxy_port: the proxy port ''' crl = pycurl.Curl() #crl.setopt(pycurl.VERBOSE,1) crl.setopt(pycurl.NOSIGNAL, 1) # set proxy rel_proxy_host = proxy_host or self.proxy_host if rel_proxy_host: crl.setopt(pycurl.PROXY, rel_proxy_host) rel_proxy_port = proxy_port or self.proxy_port if rel_proxy_port: crl.setopt(pycurl.PROXYPORT, rel_proxy_port) # set cookie rel_cookie_file = cookie_file or self.cookie_file if rel_cookie_file: crl.setopt(pycurl.COOKIEFILE, rel_cookie_file) crl.setopt(pycurl.COOKIEJAR, rel_cookie_file) # set ssl crl.setopt(pycurl.SSL_VERIFYPEER, 0) crl.setopt(pycurl.SSL_VERIFYHOST, 0) crl.setopt(pycurl.SSLVERSION, 3) crl.setopt(pycurl.CONNECTTIMEOUT, 10) crl.setopt(pycurl.TIMEOUT, 300) crl.setopt(pycurl.HTTPPROXYTUNNEL, 1) rel_header = header or self.header if rel_header: crl.setopt(pycurl.HTTPHEADER, rel_header) crl.fp = StringIO.StringIO() if isinstance(url, unicode): url = str(url) crl.setopt(pycurl.URL, url) crl.setopt(pycurl.HTTPPOST, data) # upload file crl.setopt(crl.WRITEFUNCTION, crl.fp.write) try: crl.perform() except Exception, e: raise CurlException(e)
Example #7
Source File: bagofrequests.py From pyaem with MIT License | 4 votes |
def upload_file(url, params, handlers, **kwargs): """Uploads a file using the specified URL endpoint, the file to be uploaded must be available at the specified location in file kwarg. Parameters will be appended to URL automatically on HTTP get method. Response code will then be used to determine which handler should process the response. When response code does not match any handler, an exception will be raised. :param url: URL to send HTTP request to :type url: str :param params: Request parameters key-value pairs, use array value to represent multi parameters with the same name :type params: dict :param handlers: Response handlers key-value pairs, keys are response http code, values are callback methods :type handlers: dict :param kwargs: file (str) -- Location of the file to be uploaded :type kwargs: dict :returns: PyAemResult -- The result containing status, response http code and body, and request info :raises: PyAemException """ curl = pycurl.Curl() body_io = cStringIO.StringIO() _params = [] for key, value in params.iteritems(): _params.append((key, value)) curl.setopt(pycurl.POST, 1) curl.setopt(pycurl.HTTPPOST, _params) curl.setopt(pycurl.URL, url) curl.setopt(pycurl.FOLLOWLOCATION, 1) curl.setopt(pycurl.FRESH_CONNECT, 1) curl.setopt(pycurl.WRITEFUNCTION, body_io.write) curl.perform() response = { 'http_code': curl.getinfo(pycurl.HTTP_CODE), 'body': body_io.getvalue(), 'request': { 'method': 'post', 'url': url, 'params': params } } curl.close() if response['http_code'] in handlers: return handlers[response['http_code']](response, **kwargs) else: handle_unexpected(response, **kwargs)