python基础篇 24-网络请求request

#开发好了一个接口  server

#请求接口  client

# urllib

# 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)


# requests

url = 'http://127.0.0.1:8999/login'
data = {'username':'niuhanyang2','password':'1'}
#get、post、传cookie、headers、传文件、传json、下载文件

#get请求
# r = requests.get(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'}
# r = requests.post(url,data=data,params={"version":1.0},cookies=cookie)  #params是把参数传到url后头的   上报cookie
# r = requests.post(url,data=data,params={"version":1.0},headers=headers)   # 上报headers 利用headers上报cookie


# r = requests.post(url,json=data)

# url = 'http://api.nnzhp.cn/api/file/file_upload'
# data = {'file':open('shuaige.xls','rb')}
# r = requests.post(url,files=data)    #上传文件
r = requests.get('https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=293407302,1362956882&fm=26&gp=0.jpg',verify=False)


# print(r.json() ) #->字典  #d.get('')   一般用在响应消息 字段解析
# print(r.text) #字符串格式     一般使用在html保存
print(r.content) #bytes类型的   一般用在下载的图片保存

f = open('a.jpg','wb')
f.write(r.content)
f.close()
print(r.status_code) #返回的状态码

 

posted @ 2021-12-26 20:40  捞铁  Views(125)  Comments(0Edit  收藏  举报