本文整理汇总了Python中pycurl.HTTPPOST属性的典型用法代码示例。如果您正苦于以下问题:Python pycurl.HTTPPOST属性的具体用法?Python pycurl.HTTPPOST怎么用?Python pycurl.HTTPPOST使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类pycurl
的用法示例。
在下文中一共展示了pycurl.HTTPPOST属性的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_form_poster
# 需要导入模块: import pycurl [as 别名]
# 或者: from pycurl import HTTPPOST [as 别名]
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
示例2: send_REST_request
# 需要导入模块: import pycurl [as 别名]
# 或者: from pycurl import HTTPPOST [as 别名]
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
示例3: upload_file
# 需要导入模块: import pycurl [as 别名]
# 或者: from pycurl import HTTPPOST [as 别名]
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
示例4: test_upload_file
# 需要导入模块: import pycurl [as 别名]
# 或者: from pycurl import HTTPPOST [as 别名]
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)
示例5: test_upload_file_unexpected
# 需要导入模块: import pycurl [as 别名]
# 或者: from pycurl import HTTPPOST [as 别名]
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()
示例6: upload
# 需要导入模块: import pycurl [as 别名]
# 或者: from pycurl import HTTPPOST [as 别名]
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)
示例7: upload_file
# 需要导入模块: import pycurl [as 别名]
# 或者: from pycurl import HTTPPOST [as 别名]
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)