Python uses requests to simulate HTTP requests and request headers

Source: Internet
Author: User


I. Sending requests via requests

Previously used urllib and URLLIB2 analog HTTP request sent, in the actual scenario, we need to create their own defined header, body and so on, using urllib very troublesome, very accidental opportunity to contact the requests, you can send XML, easy to use , directly on the code:

requests connected ():         #通过形参传入url以及请求参数, get method send request       (param):         get = requests.get (==param )                  #关闭连接          get.close ()                   #返回response         get.text          #通过形参传入url, test data, request header, POST method send request      ( Dataheader):         post = requests.post (=data=header)                   #关闭连接          post.close ()                 &nBSP; #返回respone         post.text 


Second, get the session, and insert the session into the header sent to the server

From com.utils.connurl import connurlimport requestsclass getheader ():      @staticmethod     def session ():          #封装测试数据         data =  ' Test data '          url = ConnUrl.getUrl  ()          url = url+ ' 200000 '      #设置header          header = {' content-type ': ' Application/octet-stream '}     #post请求         post = requests.post (url, data=data,  Headers=header)      #通过split函数切割返回数据, get session         jsessionid = post.headers[' Set-cookie '].split (';') [0]        return jsessionid

This article is from the "13015170" blog, please be sure to keep this source http://13025170.blog.51cto.com/13015170/1961183

Python uses requests to simulate HTTP requests and request headers

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.