本文整理汇总了Python中urllib2.Request.get_full_url方法的典型用法代码示例。如果您正苦于以下问题:Python Request.get_full_url方法的具体用法?Python Request.get_full_url怎么用?Python Request.get_full_url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2.Request
的用法示例。
在下文中一共展示了Request.get_full_url方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: invokeURL
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [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
示例2: fetch_darned
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def fetch_darned(self):
'''
Fetch specify raw dataest from darned.ucc.ie/static/downloads/ into the APP_ROOT/data.
Create './data' directory if APP_ROOT/data is not found.
Returns:
bool
Raises:
URLError: could not connect Darned server
'''
if os.path.isfile(self.saved_abs_path + self.filename):
sys.stderr.write("{f:s} is already exist".format(f=self.filename))
return False
req = Request(self.url)
try:
response = urlopen(req, timeout=10)
except URLError, e:
if hasattr(e, 'reason'):
sys.stderr.write('We failed to reach a server due to {0}'.format(e.reason))
raise URLError(", Could not connect " + req.get_full_url())
elif hasattr(e, 'code'):
sys.stderr.write('The server couldn\'t fulfill the request\n')
sys.stderr.write('Error code: {e}'.format(e=e.code))
raise URLError(", Could not connect " + req.get_full_url())
示例3: TestException
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
class TestException(unittest.TestCase):
def setUp(self):
self.req = Request('http://localhost/path')
def test_socket_timeout(self):
error = exc.socket.timeout('timed out')
e = exc.HTTPClientError(self.req, error)
expected = 'GET on http://localhost/path failed with socket timeout'
self.assertEquals(str(e), expected)
def test_urllib_http_error(self):
error = exc.urllib2.HTTPError(
self.req.get_full_url(), 401,
'Unauthorized', {}, StringIO("<h2>some error</h2>"))
e = exc.HTTPClientError(self.req, error)
expected = "GET on http://localhost/path returned " \
"'401' with '<h2>some error</h2>'"
self.assertEquals(str(e), expected)
def test_urllib_http_error_with_json(self):
body = {
'reason': 'Invalid Token'
}
data = json.dumps(body)
error = exc.urllib2.HTTPError(self.req.get_full_url(), 401,
'Unauthorized', {}, StringIO(data))
e = exc.HTTPClientError(self.req, error)
expected = "GET on http://localhost/path returned " \
"'401' with 'Invalid Token'"
self.assertEquals(str(e), expected)
body = {
'message': 'You supplied an invalid token.'
}
data = json.dumps(body)
error = exc.urllib2.HTTPError(self.req.get_full_url(), 401,
'Unauthorized', {}, StringIO(data))
e = exc.HTTPClientError(self.req, error)
expected = "GET on http://localhost/path returned " \
"'401' with 'You supplied an invalid token.'"
self.assertEquals(str(e), expected)
def test_urllib_url_error(self):
os_error = OSError(errno.ECONNREFUSED, os.strerror(errno.ECONNREFUSED))
socket_error = exc.socket.error(os_error)
error = exc.urllib2.URLError(socket_error)
e = exc.HTTPClientError(self.req, error)
expected = "GET on http://localhost/path failed with '%s'" % os_error
self.assertEquals(str(e), expected)
def test_bad_status_line_error(self):
error = httplib.BadStatusLine('')
e = exc.HTTPClientError(self.req, error)
expected = "GET on http://localhost/path failed " \
"with '%s'" % error.__class__.__name__
self.assertEquals(str(e), expected)
示例4: submit
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def submit(self, opener, res):
"""submit WAYF form with IDP
:param opener: the urllib2 opener
:param data: the form data as a dictionary
:param res: the response object
"""
log.info('Submitting form to wayf')
#Set IDP to correct IDP
wayf_data = {}
idp = self.idp
data = self.data
if not idp.get_idp() in data['origin']:
raise WAYFException(
"Can't find IdP '{0}' in WAYF's IdP list".format(
idp.get_idp()))
wayf_data['origin'] = data['origin'][idp.get_idp()]
wayf_data['shire'] = data['shire']['value']
wayf_data['providerId'] = data['providerId']['value']
wayf_data['target'] = data['target']['value']
wayf_data['time'] = data['time']['value']
wayf_data['cache'] = 'false'
wayf_data['action'] = 'selection'
url = urlparse.urljoin(res.url, data['form']['action'])
data = urllib.urlencode(wayf_data)
request = Request(url + '?' + data)
log.debug("POST: %s" % request.get_full_url())
response = opener.open(request)
return request, response
示例5: construct
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [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
示例6: create
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def create(self, root_endpoint, repository, authorization_headers):
url = self._create_url(
root_endpoint=root_endpoint, repository=repository)
data = self._create_data()
headers = authorization_headers.copy()
headers.update(self.headers)
request = Request(
url=url,
data=json.dumps(data).encode('UTF-8'),
headers=headers,
#method='POST',
)
_LOG.info('{method} {url}'.format(
method=request.get_method(),
url=request.get_full_url(),
))
response = urlopen(request)
info = response.info()
if info.type != 'application/json':
raise ValueError('invalid response type: {}'.format(info.type))
payload_bytes = response.read()
charset = info.getparam('charset')
payload_json = payload_bytes.decode(charset)
payload = json.loads(payload_json)
self._create_response_json(json=payload)
示例7: hit_endpoint
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def hit_endpoint(self, url, data_dict={}, http_method='GET'):
"""
A reusable method that actually performs the request to the specified Atlas API endpoint.
"""
if self.verbose == 'true':
print "HIT_ENDPOINT"
data_dict.update({ "access_token" : self.access_token })
if self.verbose == 'true':
print " Added access_token to data_dict (inside hit_endpoint)"
if self.verbose == 'true':
print " Constructing request URL"
request = Request(url, urllib.urlencode(data_dict))
if self.verbose == 'true':
print " Setting request http_method: %s" % http_method
request.get_method = lambda: http_method
try:
if self.verbose == 'true':
print " Opening Request URL: %s?%s" % (request.get_full_url(),request.get_data())
response = urlopen(request)
except URLError, e:
raise SystemExit(e)
示例8: submit
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def submit(self, opener, res):
"""submit WAYF form with IDP
:param opener: the urllib2 opener
:param data: the form data as a dictionary
:param res: the response object
"""
log.info("Submitting form to wayf")
# Set IDP to correct IDP
wayf_data = {}
idp = self.idp
data = self.data
if not idp.get_idp() in data["origin"]:
raise WAYFException("Can't find IdP '{0}' in WAYF's IdP list".format(idp.get_idp()))
wayf_data["origin"] = data["origin"][idp.get_idp()]
wayf_data["shire"] = data["shire"]["value"]
wayf_data["providerId"] = data["providerId"]["value"]
wayf_data["target"] = data["target"]["value"]
wayf_data["time"] = data["time"]["value"]
wayf_data["cache"] = "false"
wayf_data["action"] = "selection"
url = urlparse.urljoin(res.url, data["form"]["action"])
data = urllib.urlencode(wayf_data)
request = Request(url + "?" + data)
log.debug("POST: %s" % request.get_full_url())
response = opener.open(request)
return request, response
示例9: describe
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [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
示例10: getNextAgent
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def getNextAgent(self,msg):
from urllib import urlencode
from urllib2 import urlopen, Request
from config import IVRSettings as SETTINGS
#get next available agent
log.info("in CallCenterVectorManager.getNextAgent()")
#get message contents
loadedMessage = simplejson.loads(msg.body)
ccxmlSessionID = loadedMessage['session']
ccxmlUniqueID = loadedMessage['id']
#get available agents
#returns <Agent Instance>
agents = self.agentGateway.getAvailableAgentsByIdleTime()
#use our selected UCD_MIA algorithm to select the next agent
agentWithMostIdleTime = self.algo.getNextAgent(agents)
if agentWithMostIdleTime:
#create call with agent
call = self.callGateway.addCall(agent=agentWithMostIdleTime)
#update agent phone status
agentWithMostIdleTime=self.agentGateway.updateAgentPhoneStatus(agentWithMostIdleTime,1)
#inject event into ccxml session
data = urlencode({SETTINGS.IVR_SESSION_KEY:ccxmlSessionID, SETTINGS.IVR_EVENT_KEY:SETTINGS.IVR_EVENT_VALUE, \
SETTINGS.IVR_UNIQUE_ID_KEY:ccxmlUniqueID, SETTINGS.IVR_AGENT_ANI_KEY:agentWithMostIdleTime.ani, \
SETTINGS.IVR_DESTINATION_TYPE_KEY: agentWithMostIdleTime.determineANIType(), \
SETTINGS.IVR_CALL_ID_KEY: call.call_id})
url = SETTINGS.IVR_URL
request = Request(url, data)
log.info("ccxml url: " + request.get_full_url() + request.get_data())
response = urlopen(request)
if response:
log.info("Agent assigned to call sucessfully")
else:
log.info("Agent assigned to call unsucessfully")
else:
#No agent was found
#send call back to queue
publisher_args = {MQConfig.DEFAULT_ROUTING_KEY_KEY: MQConfig.DEFAULT_ROUTING_KEY_VALUE,
MQConfig.DEFAULT_DELIVERY_MODE_KEY: MQConfig.DEFAULT_DELIVERY_MODE,
MQConfig.DEFAULT_EXCHANGE_VALUE_KEY: MQConfig.DEFAULT_EXCHANGE_VALUE
}
publisher = CallCenterVectorPublisher(**publisher_args)
#setup message
msg = simplejson.dumps({'session': ccxmlSessionID , 'id': ccxmlUniqueID})
log.info("message dumped as json")
publisher.publish_to_queue(msg)
log.info("message pushed to queue")
publisher.close()
示例11: update
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def update(sensor, value, echo=False):
timestamp = datetime.now().strftime(date_format)
data = { "sensor": sensor, "value": value, "date": timestamp, "key": 101 }
if echo or debug:
print "%s: %s at %s" % (sensor, value, timestamp)
# build GET request and echo URL
req = Request("http://%s%s?%s" % (ip, update_path, urlencode(data)))
if debug:
print "GET", req.get_full_url()
# do the GET, ignore the (empty) response
if not debug:
urlopen(req).read()
示例12: node_request
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def node_request(self, node, method, path, **kwargs):
url = "http://%s:%s%s" % (node.hostname, node.port, path)
headers = {"X-Request-Id": getattr(logger.local, "request_id", "lunr-%s" % uuid4())}
if method in ("GET", "HEAD", "DELETE"):
url += "?" + urlencode(kwargs)
req = Request(url, urlencode(kwargs), headers=headers)
req.get_method = lambda *args, **kwargs: method
try:
resp = urlopen(req, timeout=self.app.node_timeout)
logger.debug("%s on %s succeeded with %s" % (req.get_method(), req.get_full_url(), resp.getcode()))
return loads(resp.read())
except (socket.timeout, urllib2.HTTPError, urllib2.URLError, HTTPException), e:
raise NodeError(req, e)
示例13: call_flickr
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def call_flickr(api_name, args):
"""
Calls a flickr api
param api_name - the api to call
args - the arguments of the api to send, in dict
return response - the response in dict format
"""
retry_limit = 3
retry_delay = 30 # seconds
request = Request(_construct_rest_url_flickr(api_name, args))
for c in range(retry_limit):
try:
response = urlopen(request)
kittens = response.read()
dict_response = json.loads(_get_json_for_response(kittens))
if "stat" in dict_response:
if dict_response["stat"] == "fail":
print ("Api request returned fail. URL", request.get_full_url())
continue
return dict_response
except URLError, e:
print "Request failed. URL: ", request.get_full_url(), "Reason:", e.reason
sleep(retry_delay) # dumb dumb just retry
示例14: parse
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [as 别名]
def parse(self, source, publicID=None, format="xml", method='POST'):
"""
Parse source into Graph
Graph will get loaded into it's own context (sub graph).
Format defaults to 'xml' (AKA: rdf/xml).
:returns: Returns the context into which the source was parsed.
:param source: source file in the form of
"http://....." or "~/dir/file.rdf"
:param publicID: *optional* the logical URI if it's different
from the physical source URI.
:param format: must be one of 'xml' or 'n3'
:param method: must be one of
* 'POST' -- method adds data to a context
* 'PUT' -- method replaces data in a context
"""
url = self.url + '/statements'
if not (source.startswith('http://') or source.startswith('file://')):
source = 'file://' + os.path.abspath(os.path.expanduser(source))
ctx = "<%s>" % (publicID or source)
url = url + "?" + urlencode(dict(context=ctx))
req = Request(url)
req.get_method = lambda: method
if format == 'xml':
req.add_header('Content-Type', 'application/rdf+xml')
elif format == 'n3':
req.add_header('Content-Type', 'text/rdf+n3')
else:
raise "Unknown format: %s" % format
req.data = urlopen(source).read()
log.debug("Request: %s" % req.get_full_url())
try:
result = urlopen(req).read()
log.debug("Result: " + result)
except HTTPError, e:
# 204 is actually the "success" code
if e.code == 204:
return
log.error(e)
raise HTTPError(e)
示例15: get_namespaces
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_full_url [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