Python Settings headers

Source: Internet
Author: User
Tags soap

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:

    1. User-agent: Some servers or proxies will use this value to determine whether a request is made by a browser
    2. Content-type: When using the REST interface, the server checks the value to determine how the content in the HTTP Body should be parsed.
    3. Application/xml: Used in XML RPC, such as Restful/soap call
    4. Application/json: Used in JSON RPC calls
    5. Application/x-www-form-urlencoded: Used when a Web form is submitted by the browser
    6. Content-type setting errors cause server denial of service when using RESTful or SOAP services provided by the server

Python Settings headers

Related Article

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.