requests模块:
import requests url = 'http://127.0.0.1:8888/login' data = {"username":"zjr","password":"1"} r = requests.get(url,data) # r = requests.post(url,data) print(type(r.json()))#返回字典 print(r.json())#返回字典 print(r.text)#返回字符串 print(r.content)#返回2进制 print(r.status_code)#返回状态码 print(r.headers) #get请求 r = requests.get(url,data) #post请求 r = requests.post(url,data) #Cookie: cookie = {'wp-settings-1':'1','PHPSESSID':'xxxxx'} headers = { 'user-agent':'xxxx', 'cookie':'wp-settings-1=libraryContent%3Dbrowse%26posts_list_mode%3Dexcerpt%26editor%3Dtinymce%26post_dfw%3Doff%26imgsize%3Dfull%26editor_plain_text_paste_warning%3D1%26hidetb%3D1; wp-settings-time-1=1573143656; comment_author_8ec14a05b6903cd9021ece26c7b908a0=111; PHPSESSID=2e33445700b8381f67cafb40ee147480'} #params是把参数传到url后头的 r = requests.post(url,data=data,params={"version":1.0},cookies=cookie)# r = requests.post(url,data=data,params={"version":1.0},headers=headers) #上传文件 url = 'http://api.nnzhp.cn/api/file/file_upload' data = {'file':open('tools.py','rb')} r = requests.post(url,files=data) print(r.text) #下载图片 url='https://pics0.baidu.com/feed/e7cd7b899e510fb30eafd55dd07a8d92d0430c11.png?token=7e3ad862ab6c3d9dd51f4a460cb4688e' r = requests.get(url,verify=False)#verify=False 可解决https报错问题 with open('a.jpg','wb') as f: f.write(r.content)
python自带的模块:
#python自带的模块,其实requests也是根据自带模块封装的 from urllib import request from urllib.parse import urlencode import requests import json #开发好了一个接口 server #请求接口 client url = 'http://127.0.0.1:8999/login?username=niuhanyang2&password=1' req = request.urlopen(url)#get请求 dic = json.loads(req.read().decode()) print(urlencode(data)) req = request.urlopen(url,urlencode(data).encode()) dic = json.loads(req.read().decode()) print(dic)