Python-based Tornado framework HTTP client tutorial, pythontornado

Source: Internet
Author: User

Python-based Tornado framework HTTP client tutorial, pythontornado

Because tornado's built-in AsyncHTTPClient function is too simple, I wrote an HTTP client library based on Tornado. Since I used this library in multiple places, I extracted it from the project, write a separate library tornadohttpclient

TornadoHTTPClient is an efficient asynchronous HTTP client library based on Tornado. It supports cookies and proxies. It has been tested only on Python2.7 platform and does not support Python3.

I listened to fairy Jun's opinion and encapsulated tornado. curl_httpclient.CurlAsyncHTTPClient directly.
Install

First, clone the code from git

git clone https://github.com/coldnight/tornadohttpclient.git

Then install it

cd tornadohttpclientpython setup.py install

Tutorial
GET

The get method of TornadoHTTPClient can initiate a get request.

From tornadohttpclient import TornadoHTTPClient # instantiate http = TornadoHTTPClient () # issue get request http. get ("http://www.linuxzen.com") # start main event loop http. start ()

POST

The TornadoHTTPClient post method can initiate a post request.
Read response

The above only sends a request, but we cannot read the data returned from the GET request. We can use a callback to read the response.

from tornadohttpclient import TornadoHTTPClienthttp = TornadoHTTPClient()def callback(response):  print response.body  http.stop()http.get("http://www.linuxzen.com", callback = callback)http.start()

Through the callback keyword parameter, we can pass in a callback function. when the request is successful, this function is called and a reponse instance with the same response as urllib2.urlopen is passed to this function.
Upload files

The upload method can upload files. It accepts a url and file field and file path, as well as other post parameters.

From tornadohttpclient import TornadoHTTPClienthttp = TornadoHTTPClient () def callback (response): print ("open image link", end = ") print (response. effective_url) http. stop () http. upload ("http://paste.linuxzen.com", "img", "img_test.png", callback = callback) http. start ()

Passing parameters to callback

Sometimes callback may need to access local variables. The callback parameter can be passed to the get/post method through the args and kwargs keyword parameters. The args parameter will be passed after the response parameter, the args parameter type should be a tuple, And the kwargs parameter type should be a dictionary

from tornadohttpclient import TornadoHTTPClienthttp = TornadoHTTPClient()def callback(response, times):  print response.body  print times  if times == 9:    http.stop()for i in range(10):  http.get("http://www.linuxzen.com", callback = callback, args = (i, ))http.start()

Send latency requests

Sometimes we need to wait several seconds to send a request or send a request every few seconds. The delay keyword parameter of the get/post method can be resolved. The delay parameter accepts a number in seconds, and initiate a request after a delay of delay seconds

From tornadohttpclient import TornadoHTTPClienthttp = TornadoHTTPClient () def callback (response, times): print response. body if times <9: # Send this request with a latency of 10 seconds http. get ("http://www.linuxzen.com", callback = callback, args = (times + 1,), delay = 10) else: http. stop () http. get ("http://www.linuxzen.com", callback = callback, args = (1,) http. start ()

Pass parameters to the request

The second parameter params of the get/post method of TornadoHTTPClient can define the type of the params parameter passed during the request as a dictionary or (key, value),) Type of tuples or lists, for example, use Baidu to search TornadoHTTPClient

from tornadohttpclient import TornadoHTTPClienthttp = TornadoHTTPClient()def callback(response):  print response.body  http.stop()http.get("http://www.baidu.com/s", (("wd", "tornado"),), callback = callback)http.start()

The above also uses the POST method, such as logging on to a website

from tornadohttpclient import TornadoHTTPClienthttp = TornadoHTTPClient()def callback(response):  print response.body  http.stop()http.post("http://ip.or.domain/login", (("username", "cold"), ("password", "pwd")), callback = callback)http.start()

Specify HTTP Header

The headers keyword parameter of the get/post method of TornadoHTTPClient can be used to customize additional HTTP header information. The parameter type is a dictionary.

Specify the User-Agent header

from tornadohttpclient import TornadoHTTPClienthttp = TornadoHTTPClient()def callback(response):  print response.body  http.stop()headers = dict((("User-Agent",        "Mozilla/5.0 (X11; Linux x86_64)"\        " AppleWebKit/537.11 (KHTML, like Gecko)"\        " Chrome/23.0.1271.97 Safari/537.11"), ))http.get("http://www.linuxzen.com", headers=headers, callback = callback)

Use proxy

The set_proxy method of TornadoHTTPClient can be set as a proxy, which accepts two parameters: the proxy host name/ip proxy port, and unset_proxy canCancel proxy

from tornadohttpclient import TornadoHTTPClienthttp = TornadoHTTPClient()def callback(response):  print response.body  http.unset_proxy()  http.stop()http.set_proxy("127.0.0.1", 8087)http.get("http://shell.appspot.com", callback = callback)http.start()

Cookie

TornadoHTTPClient automatically records and loads cookies. You can obtain cookies through TornadoHTTPClient instance attribute cookies.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.