How to use the tornado.httpclient.HTTPRequest function in tornado

To help you get started, we’ve selected a few tornado examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MLTSHP / mltshp / test / FileTests.py View on Github external
def test_adding_video_makes_it_show_up_in_friends_shake(self):
        user2 = User(name='user2', email='user2@mltshp.com', email_confirmed=1, is_paid=1)
        user2.set_password('asdfasdf')
        user2.save()
        user2.subscribe(self.user.shake())

        url = 'https://vimeo.com/20379529'
        request = HTTPRequest(self.get_url('/tools/save-video'), 'POST', {"Cookie":"sid=%s;_xsrf=%s" % (self.sid, self.xsrf)}, "url=%s&_xsrf=%s" % (url_escape(url), self.xsrf))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        sfs = Sharedfile.from_subscriptions(user2.id)
        self.assertTrue(len(sfs) > 0)
        self.assertEqual(sfs[0].name , url)
github hhru / frontik / src / test_mfd / client.py View on Github external
#coding:utf8

import time

import tornado.httpclient
import tornado.ioloop

import util

data, ct = util.make_mfd({"test": "ччч"}, {'1' : [{'filename':'1', 'body':'я'*1000 }], "2": [{'filename':'1.jpg', 'body':open("/usr/lib/openoffice/basis3.1/share/gallery/www-back/bathroom.jpg").read() }, {'filename':'1', 'body':'y'*1000 }]})
client = tornado.httpclient.AsyncHTTPClient()
req = tornado.httpclient.HTTPRequest(request_timeout=200, url='localhost:11111/', method='POST', body=data, headers={'Content-Type':ct, 'Content-Length':len(data)})

def hooray(*args, **kw):
    print args[0], time.time()

print time.time()

for i in range(10):
    client.fetch(req, hooray)

io = tornado.ioloop.IOLoop.instance()
io.start()
github paasmaker / paasmaker / paasmaker / util / apachedaemon.py View on Github external
self.assertFalse(self.server.is_running())

		# Start it again.
		self.server.start(self.stop, self.stop)
		result = self.wait()

		self.assertIn("In appropriate state", result, "Failed to start apache2 server.")

		self.assertTrue(self.server.is_running())

		# Graceful the server.
		self.server.graceful(self.stop, self.stop)
		self.wait()

		# Make sure it's still listening.
		request = tornado.httpclient.HTTPRequest('http://localhost:%d/foo' % port)
		client = tornado.httpclient.AsyncHTTPClient(io_loop=self.io_loop)
		client.fetch(request, self.stop)

		response = self.wait()
		self.assertEquals(response.code, 404, "Wrong response code.")
		self.assertTrue(len(response.body) > 0, "Didn't return a body.")

		# Also make sure it's still running.
		self.assertTrue(self.server.is_running())
github JobsDong / tigerspider / spiders / nuomi / parser.py View on Github external
input_file:File, 文件对象
            Yields:
                Item
                Task
        """
        tree = html.parse(input_file)
        citys = tree.xpath("//p[@id='citypid']/text()")
        citys = citys[0] if citys is not None and len(citys) > 0 else ""
        for city in citys.split(u","):
            city_english_name = remove_white(city)
            if len(city_english_name) > 0:
                city_item = CityItem("", city_english_name,
                                     get_city_code(city_english_name))
                if city_item.english_name and city_item.city_code:
                    yield city_item
                    http_request = HTTPRequest(
                        url=build_url_by_city_name(city_item.english_name),
                        connect_timeout=20, request_timeout=240)
                    new_task = HttpTask(http_request, callback='DealParser',
                                        max_fail_count=5,
                                        kwargs={'citycode': city_item.city_code})
                    yield new_task
github nsqio / pynsq / nsq / reader.py View on Github external
# urlsplit() is faulty if scheme not present
        if '://' not in endpoint:
            endpoint = 'http://' + endpoint

        scheme, netloc, path, query, fragment = urlparse.urlsplit(endpoint)

        if not path or path == "/":
            path = "/lookup"

        params = parse_qs(query)
        params['topic'] = self.topic
        query = urlencode(_utf8_params(params), doseq=1)
        lookupd_url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))

        req = tornado.httpclient.HTTPRequest(
            lookupd_url, method='GET',
            headers={'Accept': 'application/vnd.nsq; version=1.0'},
            connect_timeout=self.lookupd_connect_timeout,
            request_timeout=self.lookupd_request_timeout)
        callback = functools.partial(self._finish_query_lookupd, lookupd_url=lookupd_url)
        self.http_client.fetch(req, callback=callback)
github paasmaker / paasmaker / paasmaker / common / api / apirequest.py View on Github external
self.process_response(our_response)
				# And call the user defined callback back.
				if callback:
					callback(our_response)

			if not self.target:
				self.target = self.get_master()

			# Build and make the request.
			endpoint = self.target + self.get_endpoint()
			if endpoint.find('?') == -1:
				endpoint += '?format=json'
			else:
				endpoint += '&format=json'
			logger.debug("Endpoint for request: %s", endpoint)
			request = tornado.httpclient.HTTPRequest(endpoint, **kwargs)
			client = tornado.httpclient.AsyncHTTPClient(io_loop=self.io_loop)
			client.fetch(request, our_callback)
github jaedb / Iris / mopidy_iris / core.py View on Github external
def refresh_spotify_token(self, *args, **kwargs):
        callback = kwargs.get('callback', None)

        # Use client_id and client_secret from config
        # This was introduced in Mopidy-Spotify 3.1.0
        url = 'https://auth.mopidy.com/spotify/token'
        data = {
            'client_id': self.config['spotify']['client_id'],
            'client_secret': self.config['spotify']['client_secret'],
            'grant_type': 'client_credentials'
        }

        try:
            http_client = tornado.httpclient.HTTPClient()
            request = tornado.httpclient.HTTPRequest(url, method='POST', body=urllib.urlencode(data))
            response = http_client.fetch(request)

            token = json.loads(response.body)
            token['expires_at'] = time.time() + token['expires_in']
            self.spotify_token = token

            self.broadcast(data={
                'method': 'spotify_token_changed',
                'params': {
                    'spotify_token': self.spotify_token
                }
            })

            response = {
                'spotify_token': token
            }
github qq316107934 / netease-music-replacer / proxy.py View on Github external
def fetch_request(url, callback, **kwargs):
    proxy = get_proxy(url)
    if proxy:
        logger.debug('Forward request via upstream proxy %s', proxy)
        tornado.httpclient.AsyncHTTPClient.configure(
            'tornado.curl_httpclient.CurlAsyncHTTPClient')
        host, port = parse_proxy(proxy)
        kwargs['proxy_host'] = host
        kwargs['proxy_port'] = port

    req = tornado.httpclient.HTTPRequest(url, **kwargs)
    client = tornado.httpclient.AsyncHTTPClient()
    client.fetch(req, callback, raise_error=False)
github sangwonl / stage34 / webapp / modules / http / client.py View on Github external
def fetch(url, method, headers=None, body=None):
    headers.update({'User-Agent': 'Tornado/4.4.1'})
    req = HTTPRequest(url=url, method=method, headers=headers, body=body)
    http_client = AsyncHTTPClient()
    res = yield http_client.fetch(req)
    raise gen.Return(res)
github jupyter-attic / jupyterhub-carina / jupyterhub_carina / CarinaOAuthClient.py View on Github external
def execute_token_request(self, body):
        """
        Requests a new set of OAuth tokens
        """
        body.update({
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'redirect_uri': self.callback_url
        })

        request = HTTPRequest(
            url=self.CARINA_TOKEN_URL,
            method='POST',
            headers={
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body=urllib.parse.urlencode(body)
        )

        request_timestamp = time()
        response = yield self.execute_request(request)

        result = json.loads(response.body.decode('utf8', 'replace'))
        self.credentials = CarinaOAuthCredentials(
            access_token=result['access_token'],
            refresh_token=result['refresh_token'],