本文整理匯總了Python中pycurl.HTTPHEADER屬性的典型用法代碼示例。如果您正苦於以下問題:Python pycurl.HTTPHEADER屬性的具體用法?Python pycurl.HTTPHEADER怎麽用?Python pycurl.HTTPHEADER使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類pycurl
的用法示例。
在下文中一共展示了pycurl.HTTPHEADER屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _create_curl_conn
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
def _create_curl_conn(self, url):
"""
Create a cURL connection object with useful default settings.
"""
headers = []
if self.user is not None and self.secret is not None:
b64cred = base64.b64encode("{}:{}".format(self.user, self.secret))
headers.append("Authorization: Basic {}".format(b64cred))
conn = pycurl.Curl()
if len(headers) > 0:
conn.setopt(pycurl.HTTPHEADER, headers)
conn.setopt(pycurl.URL, url)
# github often redirects
conn.setopt(pycurl.FOLLOWLOCATION, 1)
return conn
示例2: perform_fp
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
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()
示例3: put
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
def put(url, data, encoding, headers=None):
"""Make a PUT request to the url, using data in the message body,
with the additional headers, if any"""
if headers is None:
headers = {}
reply = -1 # default, non-http response
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
if len(headers) > 0:
curl.setopt(pycurl.HTTPHEADER, [k + ': ' + v for k, v in list(headers.items())])
curl.setopt(pycurl.PUT, 1)
curl.setopt(pycurl.INFILESIZE, len(data))
databuffer = BytesIO(data.encode(encoding))
curl.setopt(pycurl.READDATA, databuffer)
try:
curl.perform()
reply = curl.getinfo(pycurl.HTTP_CODE)
except Exception:
pass
curl.close()
return reply
示例4: setup_curl_for_post
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
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
示例5: test_headers
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
def test_headers(self):
curl = CurlStub(b"result")
result = fetch("http://example.com",
headers={"a": "1", "b": "2"}, 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.HTTPHEADER: ["a: 1", "b: 2"],
pycurl.DNS_CACHE_TIMEOUT: 0,
pycurl.ENCODING: b"gzip,deflate"})
示例6: send_REST_request
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
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
示例7: send_REST_request
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
def send_REST_request(ip, port, object, match_key,
match_value, detail):
try:
response = StringIO()
headers = ["Content-Type:application/json"]
url = "http://%s:%s/%s" % (ip, port, object)
args_str = ''
if match_key:
args_str += match_key + "=" + match_value
if detail:
args_str += "&detail"
if args_str != '':
url += "?" + args_str
conn = pycurl.Curl()
conn.setopt(pycurl.URL, url)
conn.setopt(pycurl.HTTPHEADER, headers)
conn.setopt(pycurl.HTTPGET, 1)
conn.setopt(pycurl.WRITEFUNCTION, response.write)
conn.perform()
return response.getvalue()
except:
return None
# end def send_REST_request
示例8: send_REST_request
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
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
示例9: send_REST_request
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
def send_REST_request(ip, port, object, match_key,
match_value, detail):
try:
response = StringIO()
headers = ["Content-Type:application/json"]
url = "http://%s:%s/%s" % (ip, port, object)
args_str = ''
if match_key:
args_str += match_key + "=" + match_value
if detail:
args_str += "&detail"
if args_str != '':
url += "?" + args_str
conn = pycurl.Curl()
conn.setopt(pycurl.TIMEOUT, 1)
conn.setopt(pycurl.URL, url)
conn.setopt(pycurl.HTTPHEADER, headers)
conn.setopt(pycurl.HTTPGET, 1)
conn.setopt(pycurl.WRITEFUNCTION, response.write)
conn.perform()
return response.getvalue()
except:
return None
# end def send_REST_request
示例10: send_REST_request
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
def send_REST_request(self, server_ip, port):
try:
response = StringIO()
headers = ["Content-Type:application/json"]
url = "http://%s:%s/%s" % (server_ip, port, 'MonitorInfo')
args_str = ''
conn = pycurl.Curl()
conn.setopt(pycurl.URL, url)
conn.setopt(pycurl.HTTPHEADER, headers)
conn.setopt(pycurl.HTTPGET, 1)
conn.setopt(pycurl.WRITEFUNCTION, response.write)
conn.perform()
data_dict = response.getvalue()
data_dict = dict(json.loads(data_dict))
data_list = list(data_dict["__ServerMonitoringInfoTrace_list"]["ServerMonitoringInfoTrace"])
return data_list
except Exception as e:
print "Error is: " + str(e)
return None
# end def send_REST_request
示例11: send_REST_request
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
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
示例12: send_REST_request
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
def send_REST_request(ip, port, object, key, value, force=False):
try:
response = StringIO()
headers = ["Content-Type:application/json"]
url = "http://%s:%s/%s?%s=%s" %(
ip, port, object,
urllib.quote_plus(key),
urllib.quote_plus(value))
if force:
url += "&force"
conn = pycurl.Curl()
conn.setopt(pycurl.URL, url)
conn.setopt(pycurl.HTTPHEADER, headers)
conn.setopt(pycurl.CUSTOMREQUEST, "delete")
conn.setopt(pycurl.WRITEFUNCTION, response.write)
conn.perform()
return response.getvalue()
except:
return None
# end def send_REST_request
示例13: send_REST_request
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
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
示例14: request
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
def request(self, method, url, body=None):
def makeRequest(ignored):
curl = CurlRequestDriver.curl
curl.reset()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.HEADERFUNCTION, self.receiveHeaders)
curl.setopt(pycurl.WRITEFUNCTION, self.buffer.write)
curl.setopt(pycurl.CUSTOMREQUEST, method)
if body is not None:
curl.setopt(pycurl.POSTFIELDS, body)
headers = []
for key, value in six.iteritems(self.headers):
headers.append("{}: {}".format(key, value))
curl.setopt(pycurl.HTTPHEADER, headers)
d = threads.deferToThread(curl.perform)
d.addCallback(self.receive)
return d
def releaseLock(result):
CurlRequestDriver.lock.release()
# Forward the result to the next handler.
return result
d = CurlRequestDriver.lock.acquire()
# Make the request once we acquire the semaphore.
d.addCallback(makeRequest)
# Release the semaphore regardless of how the request goes.
d.addBoth(releaseLock)
return d
示例15: curl
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTPHEADER [as 別名]
def curl(self):
"""Sending a single cURL request to download"""
c = self._pycurl
# Resume download
if os.path.exists(self.path) and self.resume:
mode = 'ab'
self.downloaded = os.path.getsize(self.path)
c.setopt(pycurl.RESUME_FROM, self.downloaded)
else:
mode = 'wb'
with open(self.path, mode) as f:
c.setopt(c.URL, utf8_encode(self.url))
if self.auth:
c.setopt(c.USERPWD, '%s:%s' % self.auth)
c.setopt(c.USERAGENT, self._user_agent)
c.setopt(c.WRITEDATA, f)
h = self._get_pycurl_headers()
if h is not None:
c.setopt(pycurl.HTTPHEADER, h)
c.setopt(c.NOPROGRESS, 0)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(c.PROGRESSFUNCTION, self.progress)
self._fill_in_cainfo()
if self._pass_through_opts:
for key, value in self._pass_through_opts.items():
c.setopt(key, value)
c.perform()