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