Python Settings headers
Import urllib import urllib2 url = ' http://www.server.com/login ' user_agent = ' mozilla/4.0 (compatible; MSIE 5.5; Windows NT) ' values = {' username ': ' CQC ', ' password ': ' XXXX '} headers = {' User-agent ': user_agent} D ATA = Urllib.urlencode (values) request = Urllib2. Request (URL, data, headers) response = Urllib2.urlopen (Request)
In this way, we set up a headers, passed in when the request was built, and, when requested, joined the headers transfer, and the server would be responsive if it identified a request from the browser.
In addition, we have to deal with the "anti-hotlinking" way to deal with the anti-theft chain, the server will identify headers in the referer is not its own, if not, some servers will not respond, so we can also add referer in headers
For example, we can build the following headers
headers = { ' user-agent ': ' Mozilla/4.0 (compatible; MSIE 5.5; Windows NT) ', ' Referer ': ' Http://www.zhihu.com/articles '}
In addition to some of the properties of headers, the following need special attention:
- User-agent: Some servers or proxies will use this value to determine whether a request is made by a browser
- Content-type: When using the REST interface, the server checks the value to determine how the content in the HTTP Body should be parsed.
- Application/xml: Used in XML RPC, such as Restful/soap call
- Application/json: Used in JSON RPC calls
- Application/x-www-form-urlencoded: Used when a Web form is submitted by the browser
- Content-type setting errors cause server denial of service when using RESTful or SOAP services provided by the server
Python Settings headers