本文整理汇总了Python中urllib2.Request.header_items方法的典型用法代码示例。如果您正苦于以下问题:Python Request.header_items方法的具体用法?Python Request.header_items怎么用?Python Request.header_items使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2.Request
的用法示例。
在下文中一共展示了Request.header_items方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: describe
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import header_items [as 别名]
def describe(self, s_or_po, initBindings={}, initNs={}):
"""
Executes a SPARQL describe of resource
:param s_or_po: is either
* a subject ... should be a URIRef
* a tuple of (predicate,object) ... pred should be inverse functional
* a describe query string
:param initBindings: A mapping from a Variable to an RDFLib term (used
as initial bindings for SPARQL query)
:param initNs: A mapping from a namespace prefix to a namespace
"""
if isinstance(s_or_po, str):
query = s_or_po
if initNs:
prefixes = ''.join(["prefix %s: <%s>\n" % (p, n)
for p, n in initNs.items()])
query = prefixes + query
elif isinstance(s_or_po, URIRef) or isinstance(s_or_po, BNode):
query = "describe %s" % (s_or_po.n3())
else:
p, o = s_or_po
query = "describe ?s where {?s %s %s}" % (p.n3(), o.n3())
query = dict(query=query)
url = self.url + "?" + urlencode(query)
req = Request(url)
req.add_header('Accept', 'application/rdf+xml')
log.debug("opening url: %s\n with headers: %s" %
(req.get_full_url(), req.header_items()))
subgraph = ConjunctiveGraph()
subgraph.parse(urlopen(req))
return subgraph
示例2: invokeURL
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import header_items [as 别名]
def invokeURL(url, headers1, data, method):
request = Request(url, headers=headers1)
if method :
request.get_method=lambda: method
print ("Invoking URL ----" + request.get_full_url())
print ("\tmethod ----" + request.get_method())
print ("\t" + str(request.header_items()))
print ("\tInput data=" + str(data))
responseCode = 0
try:
if data :
result = urlopen(request, data)
else :
result = urlopen(request)
print (request.data)
with open("json_output.txt", "wb") as local_file:
local_file.write(result.read())
print ("\t*******OUTPUT**********" + open("json_output.txt").read())
responseCode = result.getcode()
print ("\tRESPONSE=" + str(responseCode))
print ("\t" + str(result.info()))
except URLError as err:
e = sys.exc_info()[0]
print( "Error: %s" % e)
e = sys.exc_info()[1]
print( "Error: %s" % e)
sys.exit()
except HTTPError as err:
e = sys.exc_info()[0]
print( "Error: %s" % e)
sys.exit()
print ("\tInvoking URL Complete----" + request.get_full_url())
return responseCode
示例3: construct
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import header_items [as 别名]
def construct(self, strOrTriple, initBindings={}, initNs={}):
"""
Executes a SPARQL Construct
:param strOrTriple: can be either
* a string in which case it it considered a CONSTRUCT query
* a triple in which case it acts as the rdflib `triples((s,p,o))`
:param initBindings: A mapping from a Variable to an RDFLib term (used as initial bindings for SPARQL query)
:param initNs: A mapping from a namespace prefix to a namespace
:returns: an instance of rdflib.ConjuctiveGraph('IOMemory')
"""
if isinstance(strOrTriple, str):
query = strOrTriple
if initNs:
prefixes = ''.join(["prefix %s: <%s>\n"%(p,n) for p,n in initNs.items()])
query = prefixes + query
else:
s,p,o = strOrTriple
t='%s %s %s'%((s and s.n3() or '?s'),(p and p.n3() or '?p'),(o and o.n3() or '?o'))
query='construct {%s} where {%s}'%(t,t)
query = dict(query=query)
url = self.url+"?"+urlencode(query)
req = Request(url)
req.add_header('Accept','application/rdf+xml')
log.debug("Request url: %s\n with headers: %s" % (req.get_full_url(), req.header_items()))
subgraph = ConjunctiveGraph('IOMemory')
subgraph.parse(urlopen(req))
return subgraph
示例4: get_namespaces
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import header_items [as 别名]
def get_namespaces(self):
"""Namespaces dict"""
try:
return self._namespaces
except:
pass
req = Request(self.url+'/namespaces')
req.add_header('Accept','application/sparql-results+json')
log.debug("opening url: %s\n with headers: %s" % (req.get_full_url(), req.header_items()))
import sys
if sys.version_info[0] == 3:
from io import TextIOWrapper
ret = json.load(TextIOWrapper(urlopen(req), encoding='utf8'))
else:
ret = json.load(urlopen(req))
bindings=ret['results']['bindings']
self._namespaces = dict([(b['prefix']['value'],b['namespace']['value']) for b in bindings])
return self._namespaces
示例5: get_namespaces
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import header_items [as 别名]
def get_namespaces(self):
"""Namespaces dict"""
try:
return self._namespaces
except:
pass
req = Request(self.url + "/namespaces")
req.add_header("Accept", "application/sparql-results+json")
log.debug("opening url: %s\n with headers: %s" % (req.get_full_url(), req.header_items()))
import sys
if sys.version_info[0] == 3:
from io import TextIOWrapper
ret = json.load(TextIOWrapper(urlopen(req), encoding="utf8"))
else:
ret = json.load(urlopen(req))
bindings = ret["results"]["bindings"]
self._namespaces = dict([(b["prefix"]["value"], b["namespace"]["value"]) for b in bindings])
return self._namespaces
示例6: test_connection_with_token
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import header_items [as 别名]
def test_connection_with_token():
connection = BearerAuthConnection('token', 'https://host')
connection._opener = MagicMock()
# noinspection PyProtectedMember
connection._opener.open().read.return_value = '{"hello":"world"}'
assert connection.make_request('/uri', {'it\'s': 'alive'}) == {'hello': 'world'}
request = Request('https://host/uri',
'{"it\'s": "alive"}',
headers={
'Content-type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer token'
})
capture = Capture()
# noinspection PyProtectedMember
connection._opener.open.assert_called_with(capture)
assert request.get_full_url() == capture.value.get_full_url()
assert request.header_items() == capture.value.header_items()
assert request.get_method() == capture.value.get_method()
assert request.data.encode('utf-8') == capture.value.data