Python pycurl.POST Examples
The following are 30
code examples of pycurl.POST().
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: test_fetch.py From landscape-client with GNU General Public License v2.0 | 6 votes |
def test_post(self): curl = CurlStub(b"result") result = fetch("http://example.com", post=True, curl=curl) self.assertEqual(result, b"result") self.assertEqual(curl.options, {pycurl.URL: b"http://example.com", pycurl.FOLLOWLOCATION: 1, pycurl.MAXREDIRS: 5, pycurl.CONNECTTIMEOUT: 30, pycurl.LOW_SPEED_LIMIT: 1, pycurl.LOW_SPEED_TIME: 600, pycurl.NOSIGNAL: 1, pycurl.WRITEFUNCTION: Any(), pycurl.POST: True, pycurl.DNS_CACHE_TIMEOUT: 0, pycurl.ENCODING: b"gzip,deflate"})
Example #2
Source File: smgr_reimage_server.py From contrail-server-manager with Apache License 2.0 | 6 votes |
def send_REST_request(ip, port, payload): try: response = StringIO() headers = ["Content-Type:application/json"] url = "http://%s:%s/server/reimage" %( ip, port) conn = pycurl.Curl() conn.setopt(pycurl.URL, url) conn.setopt(pycurl.HTTPHEADER, headers) conn.setopt(pycurl.POST, 1) conn.setopt(pycurl.POSTFIELDS, '%s'%json.dumps(payload)) conn.setopt(pycurl.WRITEFUNCTION, response.write) conn.perform() return response.getvalue() except: return None
Example #3
Source File: smgr_restart_server.py From contrail-server-manager with Apache License 2.0 | 6 votes |
def send_REST_request(ip, port, payload): try: response = StringIO() headers = ["Content-Type:application/json"] url = "http://%s:%s/server/restart" %( ip, port) conn = pycurl.Curl() conn.setopt(pycurl.URL, url) conn.setopt(pycurl.HTTPHEADER, headers) conn.setopt(pycurl.POST, 1) conn.setopt(pycurl.POSTFIELDS, '%s'%json.dumps(payload)) conn.setopt(pycurl.WRITEFUNCTION, response.write) conn.perform() return response.getvalue() except: return None # end def send_REST_request
Example #4
Source File: smgr_provision_server.py From contrail-server-manager with Apache License 2.0 | 6 votes |
def send_REST_request(ip, port, payload): try: response = StringIO() headers = ["Content-Type:application/json"] url = "http://%s:%s/server/provision" %( ip, port) conn = pycurl.Curl() conn.setopt(pycurl.URL, url) conn.setopt(pycurl.HTTPHEADER, headers) conn.setopt(pycurl.POST, 1) conn.setopt(pycurl.POSTFIELDS, '%s'%json.dumps(payload)) conn.setopt(pycurl.WRITEFUNCTION, response.write) conn.perform() return response.getvalue() except: return None
Example #5
Source File: sm_ansible_utils.py From contrail-server-manager with Apache License 2.0 | 6 votes |
def send_REST_request(self, ip, port, endpoint, payload, method='POST', urlencode=False): try: response = StringIO() headers = ["Content-Type:application/json"] url = "http://%s:%s/%s" %(ip, port, endpoint) conn = pycurl.Curl() if method == "PUT": conn.setopt(pycurl.CUSTOMREQUEST, method) if urlencode == False: first = True for k,v in payload.iteritems(): if first: url = url + '?' first = False else: url = url + '&' url = url + ("%s=%s" % (k,v)) else: url = url + '?' + payload if self.logger: self.logger.log(self.logger.INFO, "Sending post request to %s" % url) conn.setopt(pycurl.URL, url) conn.setopt(pycurl.HTTPHEADER, headers) conn.setopt(pycurl.POST, 1) if urlencode == False: conn.setopt(pycurl.POSTFIELDS, '%s'%json.dumps(payload)) conn.setopt(pycurl.WRITEFUNCTION, response.write) conn.perform() return response.getvalue() except: return None
Example #6
Source File: Request.py From wfuzz with GNU General Public License v2.0 | 6 votes |
def setPostData(self, pd, boundary=None): self._non_parsed_post = pd self._variablesPOST = VariablesSet() try: if self.ContentType == "multipart/form-data": self._variablesPOST.parseMultipart(pd, boundary) elif self.ContentType == 'application/json': self._variablesPOST.parse_json_encoded(pd) else: self._variablesPOST.parseUrlEncoded(pd) except Exception: try: self._variablesPOST.parseUrlEncoded(pd) except Exception: print("Warning: POST parameters not parsed") pass ############################################################################
Example #7
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 #8
Source File: server_pre_install.py From contrail-server-manager with Apache License 2.0 | 6 votes |
def send_REST_request(ip, port, object, payload): try: response = StringIO() headers = ["Content-Type:application/json"] url = "http://%s:%s/%s" %( ip, port, object) conn = pycurl.Curl() conn.setopt(pycurl.URL, url) conn.setopt(pycurl.HTTPHEADER, headers) conn.setopt(pycurl.POST, 1) conn.setopt(pycurl.POSTFIELDS, '%s'%json.dumps(payload)) conn.setopt(pycurl.CUSTOMREQUEST, "PUT") conn.setopt(pycurl.WRITEFUNCTION, response.write) conn.perform() return response.getvalue() except: return None
Example #9
Source File: server_post_install.py From contrail-server-manager with Apache License 2.0 | 6 votes |
def send_REST_request(ip, port, object, payload): try: response = StringIO() headers = ["Content-Type:application/json"] url = "http://%s:%s/%s" %( ip, port, object) conn = pycurl.Curl() conn.setopt(pycurl.URL, url) conn.setopt(pycurl.HTTPHEADER, headers) conn.setopt(pycurl.POST, 1) conn.setopt(pycurl.POSTFIELDS, '%s'%json.dumps(payload)) conn.setopt(pycurl.CUSTOMREQUEST, "PUT") conn.setopt(pycurl.WRITEFUNCTION, response.write) conn.perform() return response.getvalue() except: return None
Example #10
Source File: patator_ext.py From project-black with GNU General Public License v2.0 | 6 votes |
def perform_fp(fp, method, url, header='', body=''): #logger.debug('perform: %s' % url) fp.setopt(pycurl.URL, url) if method == 'GET': fp.setopt(pycurl.HTTPGET, 1) elif method == 'POST': fp.setopt(pycurl.POST, 1) fp.setopt(pycurl.POSTFIELDS, body) elif method == 'HEAD': fp.setopt(pycurl.NOBODY, 1) else: fp.setopt(pycurl.CUSTOMREQUEST, method) headers = [h.strip('\r') for h in header.split('\n') if h] fp.setopt(pycurl.HTTPHEADER, headers) fp.perform()
Example #11
Source File: test_fetch.py From landscape-client with GNU General Public License v2.0 | 6 votes |
def test_post_data(self): curl = CurlStub(b"result") result = fetch("http://example.com", post=True, data="data", curl=curl) self.assertEqual(result, b"result") self.assertEqual(curl.options[pycurl.READFUNCTION](), b"data") self.assertEqual(curl.options, {pycurl.URL: b"http://example.com", pycurl.FOLLOWLOCATION: 1, pycurl.MAXREDIRS: 5, pycurl.CONNECTTIMEOUT: 30, pycurl.LOW_SPEED_LIMIT: 1, pycurl.LOW_SPEED_TIME: 600, pycurl.NOSIGNAL: 1, pycurl.WRITEFUNCTION: Any(), pycurl.POST: True, pycurl.POSTFIELDSIZE: 4, pycurl.READFUNCTION: Any(), pycurl.DNS_CACHE_TIMEOUT: 0, pycurl.ENCODING: b"gzip,deflate"})
Example #12
Source File: utils.py From falsy with MIT License | 6 votes |
def setup_curl_for_post(c, p, data_buf, headers=None, share=None): setup_curl_basic(c, p, data_buf, headers, share) httpheader = p.get('httpheader', ['Accept: application/json', "Content-type: application/json"]) if httpheader: # c.setopt(pycurl.HEADER, p.get('header', 1)) c.setopt(pycurl.HTTPHEADER, httpheader) post301 = getattr(pycurl, 'POST301', None) if post301 is not None: # Added in libcurl 7.17.1. c.setopt(post301, True) c.setopt(pycurl.POST, 1) postfields = p.get('postfields') if postfields: postfields = json.dumps(postfields, indent=2, ensure_ascii=False) c.setopt(pycurl.POSTFIELDS, postfields) return c
Example #13
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 #14
Source File: patator.py From patator with GNU General Public License v2.0 | 6 votes |
def __init__(self, fd): self.rfile = fd self.error = None self.body = None command, path, version = B(self.rfile.readline()).split() self.raw_requestline = b('%s %s %s' % (command, path, 'HTTP/1.1' if version.startswith('HTTP/2') else version)) self.parse_request() self.request_version = version if self.command == 'POST': self.body = B(self.rfile.read(-1)).rstrip('\r\n') if 'Content-Length' in self.headers: del self.headers['Content-Length']
Example #15
Source File: client.py From pycopia with Apache License 2.0 | 6 votes |
def post_URL(self, obj, data, logfile=None, **kwargs): """Perform a POST method. """ obj_t = type(obj) if issubclass(obj_t, (str, urlparse.UniversalResourceLocator)): r = HTTPRequest(obj, **kwargs) r.method = "POST" r.data = data resp = r.perform(logfile) if resp.error: return [], [resp] # consistent API else: return [resp], [] elif issubclass(obj_t, HTTPRequest): obj.method = "POST" obj.data = data resp = obj.perform(logfile) return [resp], [] else: # assumed to be iterables for url, rd in itertools.izip(iter(obj), iter(data)): r = HTTPRequest(str(url), **kwargs) r.method = "POST" r.data = rd self._requests.append(r) return self.perform(logfile)
Example #16
Source File: client.py From pycopia with Apache License 2.0 | 6 votes |
def get_raw_poster(self): """Initialze a Curl object for a single POST request. This sends whatever data you give it, without specifying the content type. Returns a tuple of initialized Curl and HTTPResponse objects. """ ld = len(self._data) c = pycurl.Curl() resp = HTTPResponse(self._encoding) c.setopt(c.URL, str(self._url)) c.setopt(pycurl.POST, 1) c.setopt(c.READFUNCTION, DataWrapper(self._data).read) c.setopt(c.POSTFIELDSIZE, ld) c.setopt(c.WRITEFUNCTION, resp._body_callback) c.setopt(c.HEADERFUNCTION, resp._header_callback) headers = self._headers[:] headers.append(httputils.ContentType("")) # removes libcurl internal header headers.append(httputils.ContentLength(str(ld))) c.setopt(c.HTTPHEADER, map(str, headers)) self._set_common(c) return c, resp
Example #17
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 #18
Source File: client.py From pycopia with Apache License 2.0 | 6 votes |
def get_page(url, data=None, logfile=None, cookiefile=None, retries=1, filename=None, **kwargs): """Simple page fetcher using the HTTPRequest. Args:: url : A string or UniversalResourceLocator object. data : Dictionary of POST data (of not provided a GET is performed). logfile : A file-like object to write the transaction to. cookiefile : Update cookies in this file, if provided. retries : Number of times to retry the transaction. filename : Name of file to write target object directly to. The doc and body attributes will not be available in the response if this is given. Extra keyword arguments are passed to the HTTPRequest object. Returns:: an HTTPResponse object. """ request = HTTPRequest(url, data, **kwargs) return request.perform(logfile, cookiefile, retries, filename)
Example #19
Source File: Request.py From wfuzz with GNU General Public License v2.0 | 5 votes |
def getAuth(self): return self.__authMethod, self.__userpass # ############# TRATAMIENTO VARIABLES GET & POST #########################
Example #20
Source File: pycurllib.py From BitTorrent with GNU General Public License v3.0 | 5 votes |
def add_data(self, data): self.data = StringIO2() self.data.write(data) self.data.seek(-1) self.c.setopt(pycurl.POST, 1) self.c.setopt(self.c.READFUNCTION, self.data.read) self.c.setopt(self.c.POSTFIELDSIZE, len(data))
Example #21
Source File: pycurllib.py From BitTorrent with GNU General Public License v3.0 | 5 votes |
def add_data(self, data): self.data = StringIO2() self.data.write(data) self.data.seek(-1) self.c.setopt(pycurl.POST, 1) self.c.setopt(self.c.READFUNCTION, self.data.read) self.c.setopt(self.c.POSTFIELDSIZE, len(data))
Example #22
Source File: transport.py From termite-visualizations with BSD 3-Clause "New" or "Revised" License | 5 votes |
def request(self, url, method, body, headers): c = pycurl.Curl() c.setopt(pycurl.URL, str(url)) if 'proxy_host' in self.proxy: c.setopt(pycurl.PROXY, self.proxy['proxy_host']) if 'proxy_port' in self.proxy: c.setopt(pycurl.PROXYPORT, self.proxy['proxy_port']) if 'proxy_user' in self.proxy: c.setopt(pycurl.PROXYUSERPWD, "%(proxy_user)s:%(proxy_pass)s" % self.proxy) self.buf = StringIO() c.setopt(pycurl.WRITEFUNCTION, self.buf.write) #c.setopt(pycurl.READFUNCTION, self.read) #self.body = StringIO(body) #c.setopt(pycurl.HEADERFUNCTION, self.header) if self.cacert: c.setopt(c.CAINFO, str(self.cacert)) c.setopt(pycurl.SSL_VERIFYPEER, self.cacert and 1 or 0) c.setopt(pycurl.SSL_VERIFYHOST, self.cacert and 2 or 0) c.setopt(pycurl.CONNECTTIMEOUT, self.timeout/6) c.setopt(pycurl.TIMEOUT, self.timeout) if method=='POST': c.setopt(pycurl.POST, 1) c.setopt(pycurl.POSTFIELDS, body) if headers: hdrs = ['%s: %s' % (str(k), str(v)) for k, v in headers.items()] ##print hdrs c.setopt(pycurl.HTTPHEADER, hdrs) c.perform() ##print "pycurl perform..." c.close() return {}, self.buf.getvalue()
Example #23
Source File: patator_ext.py From project-black with GNU General Public License v2.0 | 5 votes |
def __init__(self, fd): self.rfile = fd self.error = None self.body = None self.raw_requestline = self.rfile.readline() self.parse_request() if self.command == 'POST': self.body = B(self.rfile.read(-1)).rstrip('\r\n') if 'Content-Length' in self.headers: del self.headers['Content-Length']
Example #24
Source File: __init__.py From EventGhost with GNU General Public License v2.0 | 5 votes |
def post(self, cgi, params): "Ship a POST request to a specified CGI, capture the response." self.set_option(pycurl.POST, 1) self.set_option(pycurl.POSTFIELDS, urllib_parse.urlencode(params)) return self.__request(cgi)
Example #25
Source File: test_bagofrequests.py From pyaem with MIT License | 5 votes |
def test_request_post(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) method = 'post' url = 'http://localhost:4502/.cqactions.html' params = {'foo1': 'bar1', 'foo2': ['bar2a', 'bar2b']} handlers = {200: self._handler_dummy} result = pyaem.bagofrequests.request(method, url, params, handlers) curl.setopt.assert_any_call(pycurl.POST, 1) curl.setopt.assert_any_call(pycurl.POSTFIELDS, 'foo1=bar1&foo2=bar2a&foo2=bar2b') 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 #26
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 #27
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 #28
Source File: smgr_dhcp_event.py From contrail-server-manager with Apache License 2.0 | 5 votes |
def send_REST_request(ip, port, action, payload): try: url = "http://%s:%s/dhcp_event?action=%s" % (ip, port, action) headers = ["Content-Type:application/json"] conn = pycurl.Curl() conn.setopt(pycurl.TIMEOUT, 1) conn.setopt(pycurl.URL, url) conn.setopt(pycurl.HTTPHEADER, headers) conn.setopt(pycurl.POST, 1) conn.setopt(pycurl.POSTFIELDS, payload) conn.perform() except: return
Example #29
Source File: Checkpoint.py From Instagram-API with MIT License | 5 votes |
def request(self, endpoint, headers=None, post=None, first=True): buffer = BytesIO() ch = pycurl.Curl() ch.setopt(pycurl.URL, endpoint) ch.setopt(pycurl.USERAGENT, self.userAgent) ch.setopt(pycurl.WRITEFUNCTION, buffer.write) ch.setopt(pycurl.FOLLOWLOCATION, True) ch.setopt(pycurl.HEADER, True) if headers: ch.setopt(pycurl.HTTPHEADER, headers) ch.setopt(pycurl.VERBOSE, self.debug) ch.setopt(pycurl.SSL_VERIFYPEER, False) ch.setopt(pycurl.SSL_VERIFYHOST, False) ch.setopt(pycurl.COOKIEFILE, self.settingsPath + self.username + '-cookies.dat') ch.setopt(pycurl.COOKIEJAR, self.settingsPath + self.username + '-cookies.dat') if post: import urllib ch.setopt(pycurl.POST, len(post)) ch.setopt(pycurl.POSTFIELDS, urllib.urlencode(post)) ch.perform() resp = buffer.getvalue() header_len = ch.getinfo(pycurl.HEADER_SIZE) header = resp[0: header_len] body = resp[header_len:] ch.close() if self.debug: import urllib print("REQUEST: " + endpoint) if post is not None: if not isinstance(post, list): print('DATA: ' + urllib.unquote_plus(json.dumps(post))) print("RESPONSE: " + body + "\n") return [header, json_decode(body)]
Example #30
Source File: Request.py From wfuzz with GNU General Public License v2.0 | 5 votes |
def method(self): if self._method is None: return "POST" if self._non_parsed_post is not None else "GET" return self._method