我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用tornado.httpclient()。
def get(self, *args, **kwargs): if self._access_validation is not None: self._access_validation(self.request) if self._url is None: raise tornado.web.HTTPError(404) client = tornado.httpclient.AsyncHTTPClient() r = tornado.httpclient.HTTPRequest(url=self._url, method=self.request.method, body=self.request.body, headers=self.request.headers, follow_redirects=False, allow_nonstandard_methods=True) try: return client.fetch(r, self.handle_response) except tornado.web.HTTPError as e: if hasattr(e, "response") and e.response: self.handle_response(e.response) else: raise tornado.web.HTTPError(500)
def send_request(self, request): method = request.method data = request.body headers = {'Content-Type': 'application/json'} future = TracebackFuture() def process_response_future(response): if response.exc_info() is not None: future.set_exc_info(response.exc_info()) elif response.exception() is not None: future.set_exception(response.exception()) else: result = response.result() code = result.code body = (result.body or b'').decode('utf8') future.set_result(process_response(code, body)) request = tornado.httpclient.HTTPRequest(request.url, method=method, body=data, headers=headers, request_timeout=self.config.timeout) response_future = self.http.fetch(request, raise_error=False) response_future.add_done_callback(process_response_future) return future
def _getAPISettings(): apiSettingsURL = "{0}apiSettings".format(Keys.api_url) log.debug("Getting API settings from {0}".format(apiSettingsURL)) httpClient = tornado.httpclient.HTTPClient() apiResponse = httpClient.fetch(apiSettingsURL, **fetchConfig(apiSettingsURL)) apiData = json.loads(apiResponse.body) Keys.timePermitsStorageURL = apiData.get("timePermitsStorageURL", "") Keys.managementConsoleURL = apiData.get("managementConsoleURL", "") log.debug("timpePermitsStorageURL = {0}; managementConsoleURL = {1}".format(Keys.timePermitsStorageURL, Keys.managementConsoleURL))
def detectProxy(): # Detect proxy settings for http and https from the environment # Uses http_proxy and https_proxy environment variables httpProxy = os.environ.get("HTTP_PROXY", os.environ.get("http_proxy", "")) httpsProxy = os.environ.get("HTTPS_PROXY", os.environ.get("https_proxy", "")) noProxy = os.environ.get("NO_PROXY", os.environ.get("no_proxy", "")) if httpProxy: u = urlparse(httpProxy) proxies["http"] = { "proxy_host": u.hostname or None, "proxy_port": u.port or None, "proxy_username": u.username or None, "proxy_password": u.password or None } if httpsProxy: u = urlparse(httpsProxy) proxies["https"] = { "proxy_host": u.hostname or None, "proxy_port": u.port or None, "proxy_username": u.username or None, "proxy_password": u.password or None } if httpProxy or httpsProxy: tornado.httpclient.AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient") if noProxy: proxies["noProxy"] = noProxy.split(",")
def fetchConfig(url): # Get fetch config settings for httpclient for proxy u = urlparse(url) if u.scheme in proxies: if u.hostname not in proxies.get("noProxy", []): return proxies[u.scheme] return {}
def _getTime(timeServer=None): if not timeServer: timeServer = Keys.timeServer() log.info("Getting time from {0}".format(timeServer)) httpClient = tornado.httpclient.HTTPClient() timeResponse = httpClient.fetch(timeServer, **fetchConfig(timeServer)) timeData = json.loads(timeResponse.body) certivoxClock = timeData["Time"].replace(" ", "T") certivoxTime = datetime.datetime.strptime(certivoxClock[:-1], '%Y-%m-%dT%H:%M:%S') log.debug("CertiVox Time: %s" % certivoxTime) log.debug("Local system time: %s" % datetime.datetime.utcnow()) Time.timeOffset = certivoxTime - datetime.datetime.utcnow() log.info("Synced time: %s" % (datetime.datetime.utcnow() + Time.timeOffset))
def request_sampling_strategy(self, service_name, timeout): http_client = tornado.httpclient.AsyncHTTPClient( defaults=dict(request_timeout=timeout)) # Properly url encode the params url = url_concat( 'http://%s:%d/sampling' % (self.agent_http_host, self.agent_http_port), [('service', service_name)]) return http_client.fetch(url)
def start(self): # Throw errors if backend it not properly configured if self.backend not in ('requests', 'tornado'): raise TransportException('Invalid HTTP backend: %s', self.backend) if self.backend == 'requests' and not HAS_REQUESTS: raise TransportException('Trying to use Requests as backend, but it is not installed') if self.backend == 'tornado' and not HAS_TORNADO: raise TransportException('Trying to use Tornado as backend, but it is not installed') # Prepare the tornado backend if self.backend == 'tornado': self.tornado_client = tornado.httpclient.AsyncHTTPClient(max_clients=self.max_clients) elif self.backend == 'requests': # When using requests, we start a threaded pool # with the size specified using max_clients. # Tornado already has this feature built-in. # We could just start a single thread per request, # but this leads to infinite number of threads started withing # the Publiser processes. Python is really really bad to collect # the garbage from continuous processes, and this can lead # to memory leaks. # So we'll queue the messages to be publishe self._publish_queue = queue.Queue() # TODO: use opts hwm self._pool = [] for index in range(self.max_clients): thread = threading.Thread(target=self._publish_requests) thread.daemon = True thread.start() self._pool.append(thread)
def __init__(self, config, **kwargs): self.config = config self.http = tornado.httpclient.AsyncHTTPClient(**kwargs)