Python Network programming trivia points

Source: Internet
Author: User

ImportRequestsurl='Http://127.0.0.1:8000/login'req= Requests.post (url+'?'+'username=fei&password=12')Print(req.text) #这里返回的是字符串>>>{"Code": 200,"msg":"Login Successful!"}
ImportRequestsurl='Http://127.0.0.1:8000/login'Data= {"username":"Fei","Password": 12}req=requests.post (url,data)Print(Req.text)>>>{"Code": 200,"msg":"Login Successful!"}

Import requests
url = ' Http://127.0.0.1:8000/login '
data = {"username": "Fei", "Password": 12}
req = requests.post (URL, data)
Print (Req.json ()) #返回字典格式, provided that the return must be a JSON string
>>>{' msg ': ' Login successful! ', ' Code ': $ #返回的肯定是字典, are unordered

#如何获取返回值里面的code
ImportRequestsurl="Http://127.0.0.1:8000/login"Data= {"username":"Fei","Password": 12}defGet_code (url,data): #传入链接地址, and Parameter res=requests.post (Url,data). JSON () #获取了返回值Print(RES)returnres['Code'The return value of]res is a dictionary that gets the code in the dictionary directly res "' Code '"Print(Get_code (URL, data))>>>{'Code': 200,'msg':'Login Successful!'}>>>200# prints the value of [' Code '] inside the return value

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": "The King of the continuous",
"Class": "Cancer",
"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 Network programming trivia points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.