本文整理汇总了Python中treq.client.HTTPClient类的典型用法代码示例。如果您正苦于以下问题:Python HTTPClient类的具体用法?Python HTTPClient怎么用?Python HTTPClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HTTPClient类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: validate_ticket
def validate_ticket(self, ticket, request):
service_name = self.service_name
ticket_name = self.ticket_name
this_url = self.get_url(request)
p = urlparse.urlparse(this_url)
qs_map = urlparse.parse_qs(p.query)
if ticket_name in qs_map:
del qs_map[ticket_name]
param_str = urlencode(qs_map)
p = urlparse.ParseResult(*tuple(p[:4] + (param_str,) + p[5:]))
service_url = urlparse.urlunparse(p)
params = {
service_name: service_url,
ticket_name: ticket,}
param_str = urlencode(params)
p = urlparse.urlparse(self.cas_info['service_validate_url'])
p = urlparse.ParseResult(*tuple(p[:4] + (param_str,) + p[5:]))
service_validate_url = urlparse.urlunparse(p)
log.msg("[INFO] requesting URL '%s' ..." % service_validate_url)
http_client = HTTPClient(self.agent)
d = http_client.get(service_validate_url)
d.addCallback(treq.content)
d.addCallback(self.parse_sv_results, service_url, ticket, request)
return d
开发者ID:dizzy,项目名称:txcasproxy,代码行数:27,代码来源:txcasproxy.py
示例2: validate_ticket
def validate_ticket(self, ticket, request):
service_name = self.service_name
ticket_name = self.ticket_name
this_url = self.get_url(request)
p = urlparse.urlparse(this_url)
qs_map = urlparse.parse_qs(p.query)
if ticket_name in qs_map:
del qs_map[ticket_name]
param_str = urlencode(qs_map, doseq=True)
p = urlparse.ParseResult(*tuple(p[:4] + (param_str,) + p[5:]))
service_url = urlparse.urlunparse(p)
params = {
service_name: service_url,
ticket_name: ticket,}
param_str = urlencode(params, doseq=True)
p = urlparse.urlparse(self.cas_info['service_validate_url'])
p = urlparse.ParseResult(*tuple(p[:4] + (param_str,) + p[5:]))
service_validate_url = urlparse.urlunparse(p)
self.log(
"Requesting service-validate URL => '{0}' ...".format(
service_validate_url))
http_client = HTTPClient(self.cas_agent)
d = http_client.get(service_validate_url)
d.addCallback(treq.content)
d.addCallback(self.parse_sv_results, service_url, ticket, request)
return d
开发者ID:cwaldbieser,项目名称:txcasproxy,代码行数:26,代码来源:txcasproxy.py
示例3: perform_task
def perform_task(reactor):
agent = Agent(reactor)
http = HTTPClient(agent)
d = http.put(url, auth=(admin, passwd))
d.addCallback(check_created)
d.addCallback(json_content)
d.addCallback(create_design_doc, http, scheme, host, port, db, admin, passwd)
d.addCallback(report_status)
d.addCallback(json_content)
#d.addCallback(print_result)
d.addErrback(log_error)
d.addBoth(stop, reactor)
return d
开发者ID:cwaldbieser,项目名称:txcas,代码行数:13,代码来源:setup_couchdb.py
示例4: PathResource
class PathResource(resource.Resource):
"""
Docker has asked us for the concrete on-disk location of an extant volume.
If it hasn't already asked for it to be mounted, or is currently on another
machine, this is an error.
"""
def __init__(self, *args, **kw):
self._agent = Agent(reactor) # no connectionpool
self.client = HTTPClient(self._agent)
return resource.Resource.__init__(self, *args, **kw)
def render_POST(self, request):
# TODO make a FlockerResource base class
self.base_url = os.environ.get("FLOCKER_CONTROL_SERVICE_BASE_URL")
# expect Name
data = json.loads(request.content.read())
print "path:", data
d = self.client.get(self.base_url + "/configuration/datasets")
d.addCallback(treq.json_content)
def get_dataset(datasets):
dataset_id = None
# 1. find the flocker dataset_id of the named volume
# 2. look up the path of that volume in the datasets current state
for dataset in datasets:
if dataset["metadata"]["name"] == data["Name"]:
dataset_id = dataset["dataset_id"]
d = self.client.get(self.base_url + "/state/datasets")
d.addCallback(treq.json_content)
def get_path(datasets, dataset_id):
if dataset_id is None:
path = None
else:
for dataset in datasets:
if dataset["dataset_id"] == dataset_id:
path = dataset["path"]
if path is not None:
request.write(json.dumps(dict(
Mountpoint=path,
Err=None,
)))
else:
request.write(json.dumps(dict(
Mountpoint="",
Err="unable to find %s" % (data["Name"],),
)))
request.finish()
d.addCallback(get_path, dataset_id=dataset_id)
return d
d.addCallback(get_dataset)
return server.NOT_DONE_YET
开发者ID:binocarlos,项目名称:flocker-docker-plugin,代码行数:50,代码来源:adapter.py
示例5: __init__
def __init__(self, base_url='http://localhost:8888', quiet_requests=True, **kwargs):
self.client_id = str(uuid4())
self.base_url = base_url
pool = HTTPConnectionPool(reactor, persistent=True)
agent = ContentDecoderAgent(RedirectAgent(Agent(reactor, pool=pool)), [('gzip', GzipDecoder)])
if quiet_requests:
pool._factory = QuieterHTTP11ClientFactory
auth_url = kwargs.get('auth_url')
if auth_url:
username = kwargs.get('username')
password = kwargs.get('password')
api_key = kwargs.get('api_key')
if not username:
raise RuntimeError('Marconi "auth_url" specified with no username')
if api_key:
cred = api_key
auth_type = 'api_key'
elif password:
cred = password
auth_type = 'password'
else:
raise RuntimeError('Marconi "auth_url" specified with no "password" or "api_key"')
agent = KeystoneAgent(agent, auth_url, (username, cred), auth_type=auth_type)
self.http_client = HTTPClient(agent)
开发者ID:russellhaering,项目名称:txmarconi,代码行数:30,代码来源:client.py
示例6: setUp
def setUp(self):
self.agent = mock.Mock(Agent)
self.client = HTTPClient(self.agent)
self.fbp_patcher = mock.patch('treq.client.FileBodyProducer')
self.FileBodyProducer = self.fbp_patcher.start()
self.addCleanup(self.fbp_patcher.stop)
开发者ID:fateiswar,项目名称:treq,代码行数:7,代码来源:test_client.py
示例7: main
def main():
url = "http://google.com"
factory = ssl.ClientContextFactory()
factory.protocol = LineReceiver
tor_endpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
#tls_endpoint = TLSWrapClientEndpoint(tor_endpoint, factory)
socks_agent = SOCKS5Agent(reactor, proxyEndpoint=tor_endpoint)
socks_client = HTTPClient(socks_agent)
d = socks_client.get("https://wtfismyip.com/text")
d.addCallback(readBody)
d.addCallback(foo)
reactor.run()
开发者ID:luceatnobis,项目名称:chan_archiver,代码行数:18,代码来源:txsocks_treq.py
示例8: __init__
def __init__(self, base_url, username, password):
self.base_url = base_url
self.username = username
self.password = password
self.http_client = HTTPClient(self.agent_factory(
self.clock, pool=self.pool_factory(self.clock)))
self.semaphore = defer.DeferredSemaphore(TPS_LIMIT)
开发者ID:praekelt,项目名称:junebug,代码行数:9,代码来源:rabbitmq.py
示例9: setUp
def setUp(self):
"""
Construct a fake "Docker daemon" (one which does much less than the
actual Docker daemon) and a Proxy instance.
Pre- and post-hook API servers are provided by the individual tests.
"""
self.agent = Agent(reactor) # no connectionpool
self.client = HTTPClient(self.agent)
开发者ID:mbit-cloud,项目名称:powerstrip,代码行数:9,代码来源:test_core.py
示例10: handle_outbound_message
def handle_outbound_message(self, message):
headers = {
'Content-Type': 'application/json; charset=utf-8',
}
headers.update(self.get_auth_headers())
params = {
'to_addr': message['to_addr'],
'content': message['content'],
'message_id': message['message_id'],
'in_reply_to': message['in_reply_to'],
'session_event': message['session_event']
}
if 'helper_metadata' in message:
params['helper_metadata'] = message['helper_metadata']
http_client = HTTPClient(self.agent_factory())
resp = yield http_client.put(
self.get_url('messages.json'),
data=json.dumps(params).encode('utf-8'),
headers=headers)
resp_body = yield resp.content()
if resp.code != http.OK:
log.warning('Unexpected status code: %s, body: %s' % (
resp.code, resp_body))
self.update_status(
status='down', component='submitted-to-vumi-go',
type='bad_request',
message='Message submission rejected by Vumi Go')
yield self.publish_nack(message['message_id'],
reason='Unexpected status code: %s' % (
resp.code,))
return
remote_message = json.loads(resp_body)
yield self.map_message_id(
remote_message['message_id'], message['message_id'])
self.update_status(
status='ok', component='submitted-to-vumi-go',
type='good_request', message='Message accepted by Vumi Go')
yield self.publish_ack(user_message_id=message['message_id'],
sent_message_id=remote_message['message_id'])
开发者ID:AndrewCvekl,项目名称:vumi,代码行数:43,代码来源:vumi_bridge.py
示例11: setUp
def setUp(self):
self.pool = HTTPConnectionPool(reactor, persistent=False)
self.client = HTTPClient.with_config(
pool=self.pool, allow_redirects=False, reactor=reactor)
self.resource = TxSyncMLResource(reactor)
self.site = server.Site(self.resource)
self.listener = reactor.listenTCP(0, self.site, interface='localhost')
self.listener_port = self.listener.getHost().port
self.fixtures = FixtureHelper()
self.addCleanup(self.listener.loseConnection)
self.addCleanup(self.pool.closeCachedConnections)
开发者ID:smn,项目名称:txsyncml,代码行数:11,代码来源:test_resource.py
示例12: setUp
def setUp(self):
self.agent = mock.Mock(Agent)
self.client = HTTPClient(self.agent)
self.fbp_patcher = mock.patch("treq.client.FileBodyProducer")
self.FileBodyProducer = self.fbp_patcher.start()
self.addCleanup(self.fbp_patcher.stop)
self.mbp_patcher = mock.patch("treq.multipart.MultiPartProducer")
self.MultiPartProducer = self.mbp_patcher.start()
self.addCleanup(self.mbp_patcher.stop)
开发者ID:qq40660,项目名称:treq,代码行数:11,代码来源:test_client.py
示例13: __init__
def __init__(self, host='127.0.0.1', port=8500, scheme='http',
verify=True):
self.host = host
self.port = port
self.scheme = scheme
self.base_uri = '%s://%s:%s' % (self.scheme, self.host, self.port)
self.verify = SSLSpec.CERT_NONE \
if not verify else SSLSpec.CERT_REQUIRED
agent = Agent(reactor=reactor, pool=HTTPConnectionPool(reactor),
contextFactory=AsyncClientSSLContextFactory(
verify=self.verify))
self.client = TreqHTTPClient(agent)
开发者ID:joshainglis,项目名称:python-consul,代码行数:12,代码来源:twisted.py
示例14: StreamingEliotLogsTests
class StreamingEliotLogsTests(SyncTestCase):
"""
Tests for the log streaming resources created by ``create_log_resources``.
"""
def setUp(self):
self.resource = create_log_resources()
self.agent = RequestTraversalAgent(self.resource)
self.client = HTTPClient(self.agent)
return super(StreamingEliotLogsTests, self).setUp()
def test_v1(self):
"""
There is a resource at *v1*.
"""
self.assertThat(
self.client.get(b"http:///v1"),
succeeded(has_response_code(Equals(OK))),
)
开发者ID:tahoe-lafs,项目名称:tahoe-lafs,代码行数:18,代码来源:test_logs.py
示例15: TestFakeDockerServer
class TestFakeDockerServer(TestCase):
def setUp(self):
self.dockerAPI = FakeDockerServer()
self.dockerServer = reactor.listenTCP(0, self.dockerAPI)
self.dockerPort = self.dockerServer.getHost().port
self.agent = Agent(reactor) # no connectionpool
self.client = HTTPClient(self.agent)
def tearDown(self):
return self.dockerServer.stopListening()
def test_douglas_adams_would_be_proud(self):
d = self.client.post('http://127.0.0.1:%d/towel' % (self.dockerPort,),
json.dumps({"hiding": "things"}),
headers={'Content-Type': ['application/json']})
d.addCallback(treq.json_content)
def verify(response):
self.assertEqual(response,
{"hiding": "things", "SeenByFakeDocker": 42})
d.addCallback(verify)
return d
开发者ID:mbit-cloud,项目名称:powerstrip,代码行数:21,代码来源:test_testtools.py
示例16: __init__
def __init__(self,
host='127.0.0.1',
port=8500,
scheme='http',
verify=True,
contextFactory=None,
**kwargs):
self.host = host
self.port = port
self.scheme = scheme
self.base_uri = '%s://%s:%s' % (self.scheme, self.host, self.port)
agent_kwargs = dict(
reactor=reactor, pool=HTTPConnectionPool(reactor), **kwargs)
if contextFactory is not None:
# use the provided context factory
agent_kwargs['contextFactory'] = contextFactory
elif not verify:
# if no context is provided and verify is set to false, use the
# insecure context factory implementation
agent_kwargs['contextFactory'] = InsecureContextFactory()
self.client = TreqHTTPClient(Agent(**agent_kwargs))
开发者ID:behrat,项目名称:python-consul,代码行数:23,代码来源:twisted.py
示例17: RabbitmqManagementClient
class RabbitmqManagementClient(object):
clock = reactor
@classmethod
def pool_factory(self, reactor):
pool = HTTPConnectionPool(reactor, persistent=True)
pool.maxPersistentPerHost = TPS_LIMIT
@classmethod
def agent_factory(self, reactor, pool=None):
return Agent(reactor, pool=pool)
def __init__(self, base_url, username, password):
self.base_url = base_url
self.username = username
self.password = password
self.http_client = HTTPClient(self.agent_factory(
self.clock, pool=self.pool_factory(self.clock)))
self.semaphore = defer.DeferredSemaphore(TPS_LIMIT)
def get_queue(self, vhost, queue_name):
url = 'http://%s/api/queues/%s/%s' % (
self.base_url,
urllib.quote(vhost, safe=''),
queue_name
)
def _get_queue():
d = self.http_client.get(url, auth=(self.username, self.password))
d.addCallback(treq.json_content)
return d
return self.semaphore.run(_get_queue)
开发者ID:praekelt,项目名称:junebug,代码行数:37,代码来源:rabbitmq.py
示例18: __init__
def __init__(self, dockerAddr=None, dockerPort=None, dockerSocket=None,
path='', reactor=reactor, config=None):
"""
A docker proxy resource which knows how to connect to real Docker
daemon either via socket (dockerSocket specified) or address + port for
TCP connection (dockerAddr + dockerPort specified).
"""
if config is None:
# Try to get the configuration from the default place on the
# filesystem.
self.config = PluginConfiguration()
else:
self.config = config
self.config.read_and_parse()
self.parser = EndpointParser(self.config)
Resource.__init__(self)
self.host = dockerAddr
self.port = dockerPort
self.socket = dockerSocket
self.path = path
self.reactor = reactor
proxy.ReverseProxyResource.__init__(self, dockerAddr, dockerPort, path, reactor) # NB dockerAddr is not actually used
self.agent = Agent(reactor) # no connectionpool
self.client = HTTPClient(self.agent)
开发者ID:mercykevin,项目名称:powerstrip,代码行数:24,代码来源:powerstrip.py
示例19: TestAdderPlugin
class TestAdderPlugin(TestCase):
def _getAdder(self, *args, **kw):
self.adderAPI = AdderPlugin(*args, **kw)
self.adderServer = reactor.listenTCP(0, self.adderAPI)
self.adderPort = self.adderServer.getHost().port
def setUp(self):
self.agent = Agent(reactor) # no connectionpool
self.client = HTTPClient(self.agent)
def tearDown(self):
return self.adderServer.stopListening()
def test_adder_explode(self):
"""
The adder adapter blows up (sends an HTTP 500) when asked to.
"""
self._getAdder(explode=True)
d = self.client.post('http://127.0.0.1:%d/adapter' % (self.adderPort,),
json.dumps({}),
headers={'Content-Type': ['application/json']})
def verifyResponseCode(response):
self.assertEqual(response.code, 500)
return response
d.addCallback(verifyResponseCode)
d.addCallback(treq.content)
def verify(body):
self.assertEqual(body, "sadness for you, today.")
d.addCallback(verify)
return d
def test_adder_pre(self):
"""
The adder pre-hook increments an integer according to the protocol
defined in the README.
"""
self._getAdder(pre=True)
d = self.client.post('http://127.0.0.1:%d/adapter' % (self.adderPort,),
json.dumps({
"PowerstripProtocolVersion": 1,
"Type": "pre-hook",
"ClientRequest": {
"Method": "POST",
"Request": "/fictional",
"Body": json.dumps({"Number": 7})}}),
headers={'Content-Type': ['application/json']})
def verifyResponseCode(response):
self.assertEqual(response.code, 200)
return response
d.addCallback(verifyResponseCode)
d.addCallback(treq.json_content)
def verify(body):
self.assertEqual(json.loads(body["ModifiedClientRequest"]["Body"])["Number"], 8)
d.addCallback(verify)
return d
def test_adder_post(self):
"""
The adder post-hook increments an integer according to the protocol
defined in the README.
"""
self._getAdder(post=True)
d = self.client.post('http://127.0.0.1:%d/adapter' % (self.adderPort,),
json.dumps({
"Type": "post-hook",
"ClientRequest": {
"Method": "POST",
"Request": "/fictional",
"Body": json.dumps({}),},
"ServerResponse": {
"ContentType": "application/json",
"Body": json.dumps({"Number": 7}),
"Code": 200,},
}),
headers={'Content-Type': ['application/json']})
def verifyResponseCode(response):
self.assertEqual(response.code, 200)
return response
d.addCallback(verifyResponseCode)
d.addCallback(treq.json_content)
def verify(body):
self.assertEqual(json.loads(body["ModifiedServerResponse"]["Body"])["Number"], 8)
d.addCallback(verify)
return d
开发者ID:mbit-cloud,项目名称:powerstrip,代码行数:84,代码来源:test_testtools.py
示例20: DockerProxy
class DockerProxy(proxy.ReverseProxyResource):
proxyClientFactoryClass = DockerProxyClientFactory
def __init__(self, dockerAddr=None, dockerPort=None, dockerSocket=None,
path='', reactor=reactor, config=None):
"""
A docker proxy resource which knows how to connect to real Docker
daemon either via socket (dockerSocket specified) or address + port for
TCP connection (dockerAddr + dockerPort specified).
"""
if config is None:
# Try to get the configuration from the default place on the
# filesystem.
self.config = PluginConfiguration()
else:
self.config = config
self.config.read_and_parse()
self.parser = EndpointParser(self.config)
Resource.__init__(self)
self.host = dockerAddr
self.port = dockerPort
self.socket = dockerSocket
self.path = path
self.reactor = reactor
proxy.ReverseProxyResource.__init__(self, dockerAddr, dockerPort, path, reactor) # NB dockerAddr is not actually used
self.agent = Agent(reactor) # no connectionpool
self.client = HTTPClient(self.agent)
def render(self, request, reactor=reactor):
# We are processing a leaf request.
# Get the original request body from the client.
skipPreHooks = False
if request.requestHeaders.getRawHeaders('content-type') == ["application/json"]:
originalRequestBody = request.content.read()
request.content.seek(0) # hee hee
elif request.requestHeaders.getRawHeaders('content-type') == ["application/tar"]:
# We can't JSON encode binary data, so don't even try.
skipPreHooks = True
originalRequestBody = None
else:
originalRequestBody = None
preHooks = []
postHooks = []
d = defer.succeed(None)
for endpoint in self.parser.match_endpoint(request.method, request.uri.split("?")[0]):
# It's possible for a request to match multiple endpoint
# definitions. Order of matched endpoint is not defined in
# that case.
adapters = self.config.endpoint(endpoint)
preHooks.extend(adapters.pre)
postHooks.extend(adapters.post)
def callPreHook(result, hookURL):
if result is None:
newRequestBody = originalRequestBody
else:
newRequestBody = result["ModifiedClientRequest"]["Body"]
return self.client.post(hookURL, json.dumps({
"PowerstripProtocolVersion": 1,
"Type": "pre-hook",
"ClientRequest": {
"Method": request.method,
"Request": request.uri,
"Body": newRequestBody,
}
}), headers={'Content-Type': ['application/json']})
if not skipPreHooks:
for preHook in preHooks:
hookURL = self.config.adapter_uri(preHook)
d.addCallback(callPreHook, hookURL=hookURL)
d.addCallback(treq.json_content)
def doneAllPrehooks(result):
# Finally pass through the request to actual Docker. For now we
# mutate request in-place in such a way that ReverseProxyResource
# understands it.
if result is not None:
requestBody = b""
bodyFromAdapter = result["ModifiedClientRequest"]["Body"]
if bodyFromAdapter is not None:
requestBody = bodyFromAdapter.encode("utf-8")
request.content = StringIO.StringIO(requestBody)
request.requestHeaders.setRawHeaders(b"content-length",
[str(len(requestBody))])
###########################
# The following code is copied from t.w.proxy.ReverseProxy so that
# clientFactory reference can be kept.
if not self.socket:
if self.port == 80:
host = self.host
else:
host = "%s:%d" % (self.host, self.port)
request.requestHeaders.setRawHeaders(b"host", [host])
request.content.seek(0, 0)
qs = urlparse.urlparse(request.uri)[4]
if qs:
rest = self.path + '?' + qs
else:
rest = self.path
allRequestHeaders = request.getAllHeaders()
#.........这里部分代码省略.........
开发者ID:mercykevin,项目名称:powerstrip,代码行数:101,代码来源:powerstrip.py
注:本文中的treq.client.HTTPClient类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论