本文整理汇总了Python中twisted.web.client.Agent.request方法的典型用法代码示例。如果您正苦于以下问题:Python Agent.request方法的具体用法?Python Agent.request怎么用?Python Agent.request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.client.Agent
的用法示例。
在下文中一共展示了Agent.request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getMarket
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def getMarket(card):
cached = globals()['gMarket_Cached']
if card.marketComments in cached and card.marketReviews in cached:
d1 = defer.Deferred()
d1.addCallback(lambda x: cached[card.marketComments])
d1.callback(None)
d2 = defer.Deferred()
d2.addCallback(lambda x: cached[card.marketReviews])
d2.callback(None)
return defer.DeferredList((d1,d2))
agent = Agent(reactor)
headers = {}
for k,v in standard_headers.items():
if k == 'User-Agent':
headers.update({'User-Agent':[standard_user_agents[randint(0,len(standard_user_agents)-1)]]})
else:
headers.update({k:v})
c = defer.Deferred()
c.addCallback(parseMarket, url=card.marketComments)
r = defer.Deferred()
r.addCallback(parseMarket, url=card.marketReviews)
request_comments = agent.request('GET', str(card.marketComments),Headers(),None)
request_comments.addCallback(downloadPage, c)
request_reviews = agent.request('GET', str(card.marketReviews),Headers(),None)
request_reviews.addCallback(downloadPage, r)
return defer.DeferredList((c,r))
示例2: __init__
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
class Music:
def __init__(self):
self.recenttime=0
self.player = Agent(reactor)
self.timePath = networking.musicPlayer.path("time")
def current_time(self):
"""return deferred which gets called with the current
time. This gets called really often"""
d = self.player.request("GET", self.timePath)
d.addCallback(self._timeReturned)
return d
def _timeReturned(self, response):
done = Deferred()
done.addCallback(self._bodyReceived)
response.deliverBody(GatherJson(done))
return done
def _bodyReceived(self, data):
if 't' in data:
dispatcher.send("input time", val=data['t'])
if 'song' in data and data['song']:
dispatcher.send("current_player_song", song=URIRef(data['song']))
return data['t'] # pass along to the real receiver
def playOrPause(self, t=None):
if t is None:
# could be better
self.current_time().addCallback(lambda t: self.playOrPause(t))
else:
self.player.request("POST",
networking.musicPlayer.path("seekPlayOrPause"),
bodyProducer=StringProducer(json.dumps({"t" : t})))
示例3: __init__
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
class AutoDiscover:
"""The AutoDiscover class is used to find EAS servers using only an email address"""
STATE_INIT = 0
STATE_XML_REQUEST = 1
STATE_XML_AUTODISCOVER_REQUEST = 2
STATE_INSECURE = 3
STATE_SRV = 4
STATE_REDIRECT = 5
LAST_STATE = 6
AD_REQUESTS = {STATE_XML_REQUEST:"https://%s/autodiscover/autodiscover.xml",
STATE_XML_AUTODISCOVER_REQUEST:"https://autodiscover.%s/autodiscover/autodiscover.xml",
STATE_INSECURE:"http://autodiscover.%s/autodiscover/autodiscover.xml"}
def __init__(self, email):
self.email = email
self.email_domain = email.split("@")[1]
self.agent = Agent(reactor)
self.state = AutoDiscover.STATE_INIT
self.redirect_urls = []
def handle_redirect(self, new_url):
if new_url in self.redirect_urls:
raise Exception("AutoDiscover", "Circular redirection")
self.redirect_urls.append(new_url)
self.state = AutoDiscover.STATE_REDIRECT
print "Making request to",new_url
d = self.agent.request(
'GET',
new_url,
Headers({'User-Agent': ['python-EAS-Client %s'%version]}),
AutoDiscoveryProducer(self.email))
d.addCallback(self.autodiscover_response)
d.addErrback(self.autodiscover_error)
return d
def autodiscover_response(self, result):
print "RESPONSE",result,result.code
if result.code == 302:
# TODO: "Redirect responses" validation
return self.handle_redirect(result.headers.getRawHeaders("location")[0])
return result
def autodiscover_error(self, error):
print "ERROR",error,error.value.reasons[0]
if self.state < AutoDiscover.LAST_STATE:
return self.autodiscover()
raise error
def autodiscover(self):
self.state += 1
if self.state in AutoDiscover.AD_REQUESTS:
print "Making request to",AutoDiscover.AD_REQUESTS[self.state]%self.email_domain
body = AutoDiscoveryProducer(self.email)
d = self.agent.request(
'GET',
AutoDiscover.AD_REQUESTS[self.state]%self.email_domain,
Headers({'User-Agent': ['python-EAS-Client %s'%version]}),
body)
d.addCallback(self.autodiscover_response)
d.addErrback(self.autodiscover_error)
return d
else:
raise Exception("Unsupported state",str(self.state))
示例4: HeartbeatService
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
class HeartbeatService(object):
"""
I send heartbeats to Minecraft.net/ClassiCube every so often.
"""
def __init__(self, parentService, serviceName, hburl):
self.parentService = parentService
self.serviceName = serviceName
self.hburl = hburl
self.logger = logging.getLogger("cloudbox.heartbeat.{}".format(self.serviceName))
self.agent = Agent(reactor)
self.loop = None
self.running = False
def start(self):
self.loop = LoopingCall(self.sendHeartbeat).start(30) # TODO Dynamic timeframe
self.logger.info("HeartbeatService to %s started." % self.serviceName)
self.running = True
def stop(self):
self.loop.stop()
self.logger.info("HeartbeatService to %s stopped." % self.serviceName)
self.running = False
def getMCFactoryInstance(self):
return self.parentService.factories["MinecraftHubServerFactory"]
@property
def hbdata(self):
mcFactory = self.getMCFactoryInstance()
return urllib.urlencode(
{
"port": mcFactory.settings["main"]["ports"]["clients"],
"users": len(mcFactory.clients),
"max": mcFactory.settings["main"]["max-clients"],
"name": mcFactory.settings["main"]["name"],
"public": mcFactory.settings["main"]["public"],
"version": 7,
"salt": mcFactory.settings["main"]["salt"],
}
)
def sendHeartbeat(self):
"""
Sends Heartbeat.
"""
self.agent.request(
"POST",
self.hburl,
Headers({"User-Agent": ["cloudBox %s" % VERSION], "Content-Type": ["application/x-www-form-urlencoded"]}),
_StringProducer(self.hbdata),
).addCallbacks(self.heartbeatSentCallback).addErrback(self.heartbeatFailedCallback)
def heartbeatSentCallback(self, thing):
self.logger.info("Heartbeat successfully sent to %s." % self.serviceName)
def heartbeatFailedCallback(self, failure):
self.logger.warn("Heartbeat failed to send. Error: %s" % str(failure))
示例5: handleStartSession
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def handleStartSession(self, method):
def procResponse_cb(response):
defer = Deferred()
defer.addCallback(method)
response.deliverBody(DataPrinter(defer, "bool"))
return NOT_DONE_YET
def startSession_cb((signedNonce, nonceid)):
agent = CookieAgent(Agent(reactor), self.cookie_jar)
dataq = []
dataq.append(signedNonce)
body = _FileProducer(StringIO(self.client_id.encryptData(self.client_id.password)) ,dataq)
headers = http_headers.Headers()
d = agent.request(
'PUT',
'http://localhost:8000/session/?method=startsession&ccid='
+ self.ccid + '&nonceid=' + str(nonceid),
headers,
body)
d.addCallback(procResponse_cb)
return NOT_DONE_YET
def getNonce_cb(response):
defer = Deferred()
defer.addCallback(startSession_cb)
response.deliverBody(getNonce(defer, self.client_id, self.pin))
return NOT_DONE_YET
if self.pin != None:
agent = Agent(reactor)
body = FileBodyProducer(StringIO(self.client_id.pub_key.exportKey('PEM')))
headers = http_headers.Headers()
d = agent.request(
'GET',
'http://localhost:8000/session/?method=getnonce',
headers,
body)
d.addCallback(getNonce_cb)
return NOT_DONE_YET
agent = CookieAgent(Agent(reactor), self.cookie_jar)
body = FileBodyProducer(StringIO(self.client_id.encryptData(self.client_id.password)))
headers = http_headers.Headers()
d = agent.request(
'PUT',
'http://localhost:8000/session/?method=startsession&ccid='
+ self.ccid + '&nonceid=' + str(-1),
headers,
body)
d.addCallback(procResponse_cb)
return NOT_DONE_YET
示例6: testPrimitiveTypesWithContentTypeSuffixed
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def testPrimitiveTypesWithContentTypeSuffixed(self):
"""
This test is also designed for
https://oodl.es/trac/fluiddb/ticket/572. See the comment in the
previous text for context.
In the test below, we try to PUT a primitive value type but send a
Content-type header with some trailing junk but no semicolon. Then
we do a GET and make sure that we get back the exact content type
we sent. In other words, FluidDB will have treated the value as
being opaque, as it should. This is designed to make sure that a
value PUT request sending a content-type that has our
contentTypeForPrimitiveJSON as a prefix gets treated as opaque.
"""
path = 'fluiddb/testing/test1'
objectId = yield self.createObject()
URI = '%s/%s/%s/%s' % (self.endpoint,
defaults.httpObjectCategoryName,
str(objectId),
urllib.quote(path.encode('utf-8')))
value = 5
contentType = defaults.contentTypeForPrimitiveJSON + 'blah'
try:
# PUT the value with our primitive content-type as a prefix in
# the Content-type header.
headers = Headers({
'Content-type': [contentType],
'Authorization': ['Basic %s' % b64encode(
'%s:%s' % (defaults.adminUsername.encode('utf-8'),
self.adminPassword))]})
bodyProducer = SimpleBodyProducer(json.dumps(value))
agent = Agent(reactor)
response = yield agent.request('PUT', URI,
headers, bodyProducer)
self.assertEqual(http.NO_CONTENT, response.code)
# GET the value back and check its Content-type header.
headers = Headers({
'Authorization': ['Basic %s' % b64encode(
'%s:%s' % (defaults.adminUsername.encode('utf-8'),
self.adminPassword))]})
agent = Agent(reactor)
response = yield agent.request('GET', URI, headers)
self.assertEqual(http.OK, response.code)
# Check received Content-type.
receivedContentType = response.headers.getRawHeaders(
'content-type')[0]
self.assertEqual(receivedContentType, contentType)
finally:
yield self.deleteTagValue(path, objectId)
示例7: _make_short_url
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def _make_short_url(self, long_url):
self.request_in_flight = True
api_uri = "https://git.io/"
encoded = urllib.urlencode({"url": long_url})
body_producer = StringProducer(encoded)
agent = Agent(reactor)
d = agent.request('POST', api_uri, bodyProducer=body_producer)
def onRequestComplete(data):
self._onRequestComplete()
return data
def onResponse(response):
if response.code != 201:
onRequestComplete(None)
return long_url
self._onRequestComplete()
return response.headers.getRawHeaders("Location")[-1]
d.addCallback(onResponse)
def onError(failure):
return long_url
d.addErrback(onError)
d.addErrback(onRequestComplete)
return d
示例8: get_page
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def get_page(url, method="GET", payload=None, headers=None):
"""Downloads the page from given URL, using asynchronous networking"""
agent = Agent(reactor)
producer = None
if payload:
producer = StringProducer(payload)
_headers = {"User-Agent": [settings.USER_AGENT]}
if headers:
for key, value in headers.items():
_headers[key] = [value]
response = (yield agent.request(method, str(url), Headers(_headers), producer))
# for h in response.headers.getAllRawHeaders():
# print h
try:
finished = defer.Deferred()
(yield response).deliverBody(ResponseCruncher(finished))
except:
raise Exception("Downloading page '%s' failed" % url)
defer.returnValue((yield finished))
示例9: getResource
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def getResource(path):
agent = Agent(reactor)
d = agent.request(
'GET',
path,
Headers({'User-Agent': ['playout proxy']}),
None)
def handle_response(response):
if response.code == 206:
return defer.succeed('')
else:
class SimpleReceiver(Protocol):
def __init__(s, d):
s.buf = ''
s.d = d
def dataReceived(s, data):
s.buf += data
def connectionLost(s, reason):
if response.code < 300:
s.d.callback(s.buf)
else:
s.d.errback(RuntimeError("Failed download: {} {}".format(response.code, response.phrase)))
d = defer.Deferred()
response.deliverBody(SimpleReceiver(d))
return d
d.addCallback(handle_response)
return d
示例10: requestNewPage
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def requestNewPage(headers, url, external_id):
agent = Agent(reactor)
request_d = agent.request('GET', url,Headers(headers),None)
d = defer.Deferred()
d.addCallback(parseNewPage, external_id)
request_d.addCallback(downloadPage, d)
return request_d
示例11: _upload
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def _upload(reactor, url, project, revision, revision_date, benchmark,
executable, environment, result_value, result_date, std_dev,
max_value, min_value):
data = {
'commitid': str(revision),
'revision_date': revision_date,
'project': project,
'benchmark': benchmark,
'environment': environment,
'executable': executable,
'result_value': str(result_value),
'result_date': result_date,
'std_dev': str(std_dev),
'max': str(max_value),
'min': str(min_value),
}
print('uploading', data)
agent = Agent(reactor)
d = agent.request('POST', url, None, StringProducer(urlencode(data)))
def check(response):
d = readBody(response)
def read(body):
print('body', repr(body))
if response.code != 200:
raise Exception("Upload failed: %r" % (response.code,))
d.addCallback(read)
return d
d.addCallback(check)
return d
示例12: get
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def get(self, base_url, args = {}, username = None, password = None):
#pool = HTTPConnectionPool(reactor, persistent=True)
#pool.maxPersistentPerHost = 3
#pool.retryAutomatically = False
agent = Agent(reactor)
headers = { 'User-Agent': [self._USER_AGENT_] }
if username:
authorization = b64encode(username + ":" + password)
headers['Authorization'] = [authorization]
url = self._get_url(base_url, args)
self.log.info('Requesting URL: %s' % url)
d_agent = agent.request(
'GET',
url,
Headers(headers),
None)
d_agent.addCallback(self.cb_agent)
d_agent.addErrback(self.cb_agent_err)
self.d = defer.Deferred()
return self.d
示例13: _post_api_twisted
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def _post_api_twisted(path, payload, access_token=None):
headers = {'Content-Type': ['application/json']}
if access_token is not None:
headers['X-Rollbar-Access-Token'] = [access_token]
# Serialize this ourselves so we can handle error cases more gracefully
payload = ErrorIgnoringJSONEncoder().encode(payload)
url = urlparse.urljoin(SETTINGS['endpoint'], path)
agent = TwistedHTTPClient(reactor, connectTimeout=SETTINGS.get('timeout', DEFAULT_TIMEOUT))
resp = yield agent.request(
'POST',
url,
TwistedHeaders(headers),
StringProducer(payload))
r = requests.Response()
r.status_code = resp.code
r.headers.update(resp.headers.getAllRawHeaders())
bodyDeferred = Deferred()
resp.deliverBody(ResponseAccumulator(resp.length, bodyDeferred))
body = yield bodyDeferred
r._content = body
_parse_response(path, SETTINGS['access_token'], payload, r)
yield returnValue(None)
示例14: _process_queue
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def _process_queue(self):
if self.searches.under_processing:
return False
if not self.searches.request_queue:
return False
self.searches.under_processing = True
self.searches.errors = {}
self.searches.tweets = []
self.searches.got_twt_data = []
self.searches.processed_request = self.searches.request_queue.pop(0)
self.tweet_count = 0
debug_msg(str(self.searches.processed_request['search_spec']))
auth_sys = AuthProcessor()
params_urlized = auth_sys.get_search_params(self.searches.processed_request['search_spec'])
req_headers = auth_sys.get_headers(params_urlized)
contextFactory = TwtClientContextFactory()
agent = Agent(reactor, contextFactory)
d = agent.request(
'GET',
'https://api.twitter.com/1.1/search/tweets.json' + '?' + params_urlized,
Headers(req_headers),
None)
borders = TwtResponseBorders(self, self.searches)
d.addCallback(borders.cbRequest)
d.addBoth(borders.cbShutdown)
return True
示例15: test_round_trip
# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import request [as 别名]
def test_round_trip(self):
d = Deferred()
self.worker.deliver_hook = lambda x: d.callback(None)
self.worker.startWorker()
self.test_worker.startWorker()
params = {
'username': 'user',
'password': 'pass',
'owner': 'owner',
'service': 'service',
'subservice': 'subservice',
'call-number': '+27831234567',
'origin': '12345',
'messageid': 'message_id',
'provider': 'provider',
'tariff': 0,
'text': 'message content',
}
agent = Agent(reactor)
response = yield agent.request(
'POST', self.config['url'], Headers({
'User-Agent': ['Vumi Vas2Net Transport'],
'Content-Type': ['application/x-www-form-urlencoded'],
}), StringProducer(urlencode(params)))
log.msg('Headers', list(response.headers.getAllRawHeaders()))
self.assertTrue(response.headers.hasHeader('X-Nth-Smsid'))
yield d