本文整理汇总了Python中urllib.request.Request.add_data方法的典型用法代码示例。如果您正苦于以下问题:Python Request.add_data方法的具体用法?Python Request.add_data怎么用?Python Request.add_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.request.Request
的用法示例。
在下文中一共展示了Request.add_data方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _send_rpc
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def _send_rpc(self, method, params=[]):
request_data = json.dumps({'id': 0,
'method': method,
'params': params})
req = Request(self.rpc_host)
req.add_header("Authorization", "Basic %s" % self.auth)
req.add_header("Content-Type", "text/plain")
req.add_data(bytes(request_data, 'ascii'))
try:
file = urlopen(req)
result = file.read()
file.close()
result = json.loads(str(result, 'ascii'))
except HTTPError as e:
try:
result = e.read()
result = json.loads(str(result, 'ascii'))
except:
logger.error(result)
raise e
if result['error']:
raise RPCError(result['error'])
return result['result']
示例2: __open
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def __open(self, url, headers={}, data=None, baseurl=""):
"""Raw urlopen command"""
if not baseurl:
baseurl = self.baseurl
req = Request("%s%s" % (baseurl, url), headers=headers)
try:
req.data = urlencode(data).encode('utf-8') # Python 3
except:
try:
req.add_data(urlencode(data)) # Python 2
except:
pass
# Proxy support
if self.proxy_url is not None:
if self.proxy_user is None:
handler = ProxyHandler({'https': self.proxy_url})
opener = build_opener(handler)
else:
proxy = ProxyHandler({'https': 'https://%s:%[email protected]%s' % (self.proxy_user,
self.proxy_password, self.proxy_url)})
auth = HTTPBasicAuthHandler()
opener = build_opener(proxy, auth, HTTPHandler)
resp = opener.open(req)
else:
resp = urlopen(req)
charset = resp.info().get('charset', 'utf-8')
return json.loads(resp.read().decode(charset))
示例3: access_url
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def access_url(parr, post=None, json_response=None):
url = 'http://localhost:8000/{0}'.format(parr[0])
status_code = parr[1]
rest = parr[2] if len(parr) > 2 else None
headers = {
'User-Agent': user_agent
}
if rest is not None and 'eTag' in rest:
headers["if-none-match"] = rest['eTag']
req = Request(url, headers=headers)
if post is not None:
post_str = json.dumps(post, indent=2, sort_keys=True, allow_nan=False)
req.add_header("Content-Type", "application/json")
req.add_header("Content-Length", len(post_str))
req.add_data(post_str)
try:
response = urlopen(req)
except HTTPError as e:
response = e
if rest is not None and 'url' in rest:
expected_url = 'http://localhost:8000/{0}'.format(rest['url'])
if response.geturl() != expected_url:
status("HEADERS:\n{0}\nBODY:\n{1}\n", response.headers, response.read()) # pragma: no cover
return fail("redirection failed! expected '{0}' got '{1}'", expected_url, response.geturl()) # pragma: no cover
if response.code != status_code:
status("HEADERS:\n{0}\nBODY:\n{1}\n", response.headers, response.read()) # pragma: no cover
return fail("{0} responded with {1} ({2} expected)", url, response.code, status_code) # pragma: no cover
if json_response is not None:
json_response["response"] = json.loads(response.read())
return True
示例4: add_event
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def add_event(self, what, data=None, tags=None, when=None):
"""
:param what:
:param data:
:param tags:
:param when:
:return:
"""
if not when:
when = time.time()
postdata = '{{"what":"{0}", "when":{1}'.format(what, when)
if data:
postdata += ', "data":"{0}"'.format(str(data))
if tags:
postdata += ', "tags": "{0}"'.format(str(tags))
postdata += '}'
req = Request(self.url_post_event)
req.add_data(postdata)
try:
urlopen(req)
except Exception as _:
# log.exception('Error when sending event to carbon')
pass
示例5: get_http_data
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def get_http_data(url, method="GET", header="", data=""):
""" Get the page to parse it for streams """
request = Request(url)
request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
if len(header) > 0:
request.add_header('Content-Type', header)
if len(data) > 0:
request.add_data(data)
try:
response = urlopen(request)
except HTTPError as e:
log.error("Something wrong with that url")
log.error("Error code: %s" % e.code)
sys.exit(5)
except URLError as e:
log.error("Something wrong with that url")
log.error("Error code: %s" % e.reason)
sys.exit(5)
except ValueError as e:
log.error("Try adding http:// before the url")
sys.exit(5)
if sys.version_info > (3, 0):
data = response.read().decode('utf-8')
else:
try:
data = response.read()
except socket.error as e:
log.error("Lost the connection to the server")
sys.exit(5)
response.close()
return data
示例6: _try_server
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def _try_server(self, store_url, path, query_string, data, content_type,
content_length):
request = Request(str(store_url + path + query_string))
if data is not None:
request.add_header('Content-Type', content_type)
request.add_header('Content-Length', content_length)
try:
request.add_data
except AttributeError:
request.data = data # python3
else:
request.add_data(data) # python2
file = urlopen(request)
status = file.getcode()
assert status in (200, 204) # OK, OK-no-data
# Break early if we're dealing with no-data.
if status == 204:
try:
nothing = file.read()
assert len(nothing) == 0, nothing
finally:
file.close()
data = None
# JSON data?
elif path.endswith('.js'):
try:
data = from_jsonfile(utf8_reader(file))
finally:
file.close()
# Binary data?
elif path.endswith('.bin'):
# We expect this to be set!
try:
file.headers.getheader
except AttributeError: # py3
content_length = file.headers.get('content-length')
enctype = file.headers.get('x-encryption', 'none')
else: # py2
content_length = file.headers.getheader('content-length')
enctype = file.headers.getheader('x-encryption', 'none')
finally:
length = int(content_length)
# No file closing here.. the cryptoreader gets to use it.
data = CryptoReader(fp=file, length=length, enctype=enctype)
# Unknown data?
else:
file.close()
raise NotImplementedError(path)
return data
示例7: RequestTests
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
class RequestTests(unittest.TestCase):
def setUp(self):
self.get = Request("http://www.python.org/~jeremy/")
self.post = Request("http://www.python.org/~jeremy/",
"data",
headers={"X-Test": "test"})
def test_method(self):
self.assertEqual("POST", self.post.get_method())
self.assertEqual("GET", self.get.get_method())
def test_add_data(self):
self.assertFalse(self.get.has_data())
self.assertEqual("GET", self.get.get_method())
self.get.add_data("spam")
self.assertTrue(self.get.has_data())
self.assertEqual("POST", self.get.get_method())
def test_get_full_url(self):
self.assertEqual("http://www.python.org/~jeremy/",
self.get.get_full_url())
def test_selector(self):
self.assertEqual("/~jeremy/", self.get.get_selector())
req = Request("http://www.python.org/")
self.assertEqual("/", req.get_selector())
def test_get_type(self):
self.assertEqual("http", self.get.get_type())
def test_get_host(self):
self.assertEqual("www.python.org", self.get.get_host())
def test_get_host_unquote(self):
req = Request("http://www.%70ython.org/")
self.assertEqual("www.python.org", req.get_host())
def test_proxy(self):
self.assertFalse(self.get.has_proxy())
self.get.set_proxy("www.perl.org", "http")
self.assertTrue(self.get.has_proxy())
self.assertEqual("www.python.org", self.get.get_origin_req_host())
self.assertEqual("www.perl.org", self.get.get_host())
def test_wrapped_url(self):
req = Request("<URL:http://www.python.org>")
self.assertEqual("www.python.org", req.get_host())
def test_urlwith_fragment(self):
req = Request("http://www.python.org/?qs=query#fragment=true")
self.assertEqual("/?qs=query", req.get_selector())
req = Request("http://www.python.org/#fun=true")
self.assertEqual("/", req.get_selector())
示例8: workout_delete
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def workout_delete(self, workout_id):
token = self._get_auth_token(self._import_form_url())
headers = {}
headers.update(self._CHEAT_HEADERS)
delete_request = Request(url=self._workout_url(workout_id))
data = urlencode({
'_method': 'delete',
'authenticity_token': token
}).encode("utf-8")
delete_request.add_data(data)
response = self._opener.open(delete_request)
assert response.getcode() / 100 == 2
示例9: __open
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def __open(self, url, headers={}, data=None):
"""Raw urlopen command"""
req = Request("%s%s" % (self.url, url), headers=headers)
try:
req.data = urlencode(data).encode('utf-8') # Python 3
except:
try:
req.add_data(urlencode(data)) # Python 2
except:
pass
resp = urlopen(req)
charset = resp.info().get('charset', 'utf-8')
return json.loads(resp.read().decode(charset))
示例10: build_request
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def build_request(self, endpoint, data = None):
'''Expects an endpoint like 'image.json' or a tuple like ('gallery', 'hot', 'viral', '0').
Prepends 3/ and appends \.json to the tuple-form, not the endpoint form.'''
if isinstance(endpoint, str):
url = self.API_URL + endpoint
else:
url = self.API_URL + '3/' + ('/'.join(endpoint)) + ".json"
req = UrlLibRequest(url)
if data is not None:
req.add_data(UrlLibEncode(data).encode('utf-8'))
return req
示例11: _do_auth
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def _do_auth(self, user, password, token):
self._cookie_jar = CookieJar()
self._opener = build_opener(HTTPCookieProcessor(self._cookie_jar))
auth_request = Request(url=self.auth_url())
data = urlencode({
'user[email]': user,
'user[password]': password,
'authenticity_token': token}).encode("utf-8")
auth_request.add_data(data)
response = self._opener.open(auth_request)
assert response.getcode() / 100 == 2
soup = BeautifulSoup(response.read())
profile_tags = soup.findAll(name='li', attrs={'class': 'profile'})
self._user_id = profile_tags[0].a['href'].split('/')[-1].split('?')[0]
示例12: _get_access_token
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def _get_access_token(self):
"""Obtain a bearer token."""
bearer_token = '%s:%s' % (self.consumer_key, self.consumer_secret)
encoded_bearer_token = base64.b64encode(bearer_token.encode('ascii'))
request = Request(REQUEST_TOKEN_URL)
request.add_header('Content-Type',
'application/x-www-form-urlencoded;charset=UTF-8')
request.add_header('Authorization',
'Basic %s' % encoded_bearer_token.decode('utf-8'))
request.add_data('grant_type=client_credentials'.encode('ascii'))
response = urlopen(request)
raw_data = response.read().decode('utf-8')
data = json.loads(raw_data)
return data['access_token']
示例13: _http
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def _http(self, url, method, data=None, decoder=None, parse_json=True):
res = None
req = Request(url=url)
req.add_header('Content-Type', 'application/json')
req.add_header('Hawkular-Tenant', self.tenant_id)
req.add_header('Host', self.host)
if self.token is not None:
req.add_header('Authorization', 'Bearer {0}'.format(self.token))
elif self.username is not None:
b64 = base64.b64encode((self.username + ':' + self.password).encode('utf-8'))
req.add_header('Authorization', 'Basic {0}'.format(b64.decode()))
if self.authtoken is not None:
req.add_header('Hawkular-Admin-Token', self.authtoken)
if not isinstance(data, str):
data = json.dumps(data, indent=2)
reader = codecs.getreader('utf-8')
if data:
try:
req.add_data(data)
except AttributeError:
req.data = data.encode('utf-8')
try:
req.get_method = lambda: method
res = urlopen(req, context=self.context)
if parse_json:
if res.getcode() == 200:
data = json.load(reader(res), cls=decoder)
elif res.getcode() == 204:
data = {}
else:
data = reader(res).read()
return data
except Exception as e:
self._handle_error(e)
finally:
if res:
res.close()
示例14: __request
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def __request(self, uri, getParameters=None, postParameters=None):
from urllib.request import Request
address = self.address + uri
if getParameters:
address += '?' + '&'.join(key + '=' + value for key, value in self.__encodeParameters(getParameters))
request = Request(address)
if postParameters is not None:
request.add_data(json.dumps(postParameters).encode('utf-8'))
if self.username:
from base64 import urlsafe_b64encode
authenticationString = self.username + ':' + self.password
authenticationString = urlsafe_b64encode(authenticationString.encode('ascii')).decode('ascii')
request.add_header('Authorization', 'Basic ' + authenticationString)
elif self.token:
request.add_header('Authorization', 'Token token=' + self.token)
request.add_header('Content-type', 'application/json')
return request
示例15: ws_request
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_data [as 别名]
def ws_request(url, post=False, **kwargs):
url_values = urlencode(kwargs)
url = url + '?' + url_values
logger.debug('Accessing URL %s' % url)
req = Request(url)
if post:
req.add_data(post)
req.add_header('Accept', '*/*')
try:
return urlopen(req)
except HTTPError as e:
if e.code == 404:
raise NotFound(url)
else:
raise e