码迷,mamicode.com
首页 > 编程语言 > 详细

python网络编程小知识点

时间:2017-11-24 16:58:35      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:中文   链接地址   user   inxi   post   报文   字符   pass   pytho   

import requests
url = http://127.0.0.1:8000/login
req = requests.post(url+?+username=fei&password=12)
print(req.text)#这里返回的是字符串
>>>{"code": 200, "msg": "登录成功!"}
import requests
url = http://127.0.0.1:8000/login
data = {"username":"fei","password":12}
req = requests.post(url,data)
print(req.text)
>>>{"code": 200, "msg": "登录成功!"}

import requests
url = ‘http://127.0.0.1:8000/login‘
data = {"username": "fei", "password": 12}
req = requests.post(url, data)
print(req.json())#返回字典格式,前提是返回必须是json串
>>>{‘msg‘: ‘登录成功!‘, ‘code‘: 200}#返回的肯定是字典,都是无序的

#如何获取返回值里面的code
import
requests url = "http://127.0.0.1:8000/login" data = {"username": "fei", "password": 12} def get_code(url,data):#传入链接地址,和参数 res = requests.post(url,data).json()#获取了返回值 print(res) return res[code]res的返回值是一个字典,获取字典里面的code就直接res【‘code’】 print(get_code(url, data)) >>>{code: 200, msg: 登录成功!} >>>200#打印出返回值里面的[‘code‘]的值

r = requests.post(‘http://127.0.0.1:8000/add_stu‘,json=data2)  #请求报文为json类型的
print(r.text)
#发带cookie的 header
sign = get_sign(url,data)
cookie = {‘sign‘:sign}
header = {‘touxinxi‘:‘hahahah‘}
data2 = {
"username":‘william‘,
"real_name":"王绵绵",
"class":"巨蟹座",
"phone":"19312345674"
}
url = "http://127.0.0.1:8000/add_stu2"
r = requests.post(url,json=data2,cookies=cookie,headers=header) #header里面传的时候不能有中文
print(r.text)


#下面是上传文件的
file ={
‘file_name‘:open(‘hahah.py‘,encoding=‘utf-8‘)
}
data = {"username":"fei",‘pwd‘:12}
url="http://127.0.0.1:8000/upload"
r = requests.post(url,data,files=file)
print(r.text)

python网络编程小知识点

标签:中文   链接地址   user   inxi   post   报文   字符   pass   pytho   

原文地址:http://www.cnblogs.com/wmm007/p/7891046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!