1. Get request
#-*-coding:utf-8-*-Importrequestsurl_ip="http://b.com/index.php"Pyload= {'Cate': 1,'ID': 2}headers= {'user-agent':'mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/60.0.3112.101 safari/537.36'}defuse_simple_requests (): R= Requests.get (Url_ip, Params=pyload, headers=headers)PrintR.url#resuest URL PrintR.headers#Response Header PrintR.text#response content (after encoding) PrintR.content#Response Content (no encoding) PrintR.status_code#HTTP status Code PrintR.json ()#output JSON data, the server returned is not JSON content will be error #print R.raw.read () #获取原始套接字相应, the request needs to be set Stream=trueuse_simple_requests ()
2. Post request
#-*-coding:utf-8-*-Importrequestsurl_ip="http://b.com/index.php"Pyload= {'Cate': 1,'ID': 2}defuse_simple_requests (): R= Requests.post ('Http://httpbin.org/post', Data=pyload)Printr.textuse_simple_requests ()
3. Cookies
Server side: http://a.com/index.php
<? PHP $cookie _name $_cookie [' name ']; Accept the cookie sent by the clientsetcookie(' name ',$cookie _name. 777 '); Send the accepted cookie value to the client after appending ' 777 ' ?>
Client: request_demo.py
#-*-coding:utf-8-*-Importrequestsurl_ip="http://b.com/index.php"Cookies= {'name':'testtest'}#Set Cookiesdefuse_simple_requests (): R= Requests.get (url_ip, cookies=cookies)#bring a cookie on request Printr.headersPrintr.cookies['name']#Print Cookiesuse_simple_requests ()
Documentation: http://docs.python-requests.org/en/master/user/quickstart/#make-a-request
[Python learning] use of the requests library