Collect client-related information based on HTTP header

Source: Internet
Author: User

"Course" web2.0 programming
"Job Requirements" use Tornado to write a small program that collects client-related information based on the HTTP header: Whether it's a phone, operating system, browser, etc. *.
"Job tip" mainly resolves Request header["user-agent". The string format can be found through a wiki link; Through Tornado English website documents, know to obtain with self.request
An instance of the Tornado.httputil.HTTPServerRequest object that has properties of the headers dictionary type.
"Reference document" User-agent definition and string format (wiki) tornado related strings and representative version information commonly found in English web site documents

"Lab Environment" operating system: Ubantu 13.10 Browser: Firefox python:2.7.5+ tornado:1.2.1 version

1. According to the Tonardo English Web site documentation, learn about the use of self.request to obtain information about the client. In this case, I will first list some of the more important attributes of the request and make an interpretation

Method
HTTP Request method: Get or Post

Uri
The requested URI, the Uniform Resource identifier, used to uniquely identify a resource (about URI)

Path
The path portion of the URI

Query
Request part of Uri

Version
Version of the HTTP protocol, such as "http/1.1"

Headers
This is a dictionary-type property with many rich information, HTTP headers is the HTTP request and the corresponding core, it carries information about the client browser, the request page, the server and so on.

Let's take a look at what's in it:
' Accept-language ': ' zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 ',//indicates the default language setting for the browser, q indicates priority, and the higher the Q value the greater the level
' accept-encoding ': ' gzip, deflate ',//means the browser supports compression mode
' Connection ': ' keep-alive ',//indicates that the browser and host remain connected
' Accept ': ' text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ',//indicates the MIME type supported by the browser
' User-agent ': ' mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) gecko/20100101 firefox/29.0 ',//Browser identifier (operating system identity; encryption level identification; browser language) render engine identity version information
' Host ': ' localhost:8000 ',//host name
' Cookie ': ' User=zmzm|1417654344|5de24afe2a1e8c302f29211dfd4a726186bdc9af ',//A small amount of user information stored by the client
' Cache-control ': ' max-age=0 '//Response cache's effective second speed

headers[' User-agent ']
Browser identification (operating system identity; encryption level identification; browser language) render engine identity version information

Common related string and representative version information

Body
The body of the request content, if present, is a string type.

Remote_ip
A string representing the IP address of the client.

Protocol
Protocol that the program complies with

Host
The host name being requested

Arguments
Used to store a dictionary of parameters that represent Get/post

Files
Uploaded files

Connection
A single connection can connect multiple requests through which the HTTP request can be accessed

2. The following is the core code of my applet (named t1.py):

#Coding:utf-8ImportTornado.httpserverImportTornado.ioloopImporttornado.optionsImportTornado.web fromTornado.httpclientImportasynchttpclient fromTornado.optionsImportdefine, Optionsdefine ("Port", default=8000, help="run on the given port", type=int)classIndexhandler (tornado.web.RequestHandler):defGet (self):Print "**************now Let me show you something about httpserverrequest**********"        Print "#method #:"              PrintSelf.request.methodPrint "#uri #:"        PrintSelf.request.uriPrint "#path #:"        PrintSelf.request.pathPrint "#query #:"        PrintSelf.request.queryPrint "#version #:"        Printself.request.versionPrint "#headers #:"        Printself.request.headersPrint "#headers [' user-agent ']#:"        Printself.request.headers['user-agent']        Print "#body #:"        PrintSelf.request.bodyPrint "#remote_ip #:"        Printself.request.remote_ipPrint "#protocol #:"        PrintSelf.request.protocolPrint "#host #:"        PrintSelf.request.hostPrint "#arguments #:"        Printself.request.argumentsPrint "#files #:"        PrintSelf.request.filesPrint "#connection #:"        Printself.request.connectionif __name__=="__main__": Tornado.options.parse_command_line () application=tornado.web.Application (Handlers= [(R"/", Indexhandler)]) Http_server=tornado.httpserver.HTTPServer (application) Http_server.listen (Options.port) tornado.ioloop.IOLoop.instance ( ). Start ()

Open terminal and enter the following command line:

~$ python t1.py

Open the browser and open the link: localhost:8000

The following output can be seen at the terminal at this time

From headers[' user-agent ') can be seen, in my experiment,

Operating system is Linux, browser is Firefox, version is 29.0 which Gecko is the rendering engine of Firefox, version is 20100101

Collect client-related information based on HTTP header

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.