Python Learning Notes (18) Useful module introduction in network programming

Source: Internet
Author: User

In the previous blog we use the Python urllib module to request a website, or interface, but the Urllib module is too troublesome, pass parameters, all have to be bytes type, return data is also bytes type, but also to decode, want to directly return the results to use, Also need to use JSON, send GET request and POST request, also do not pass, use more trouble, there is a more convenient module, more convenient than urllib module, is the requests module, it is more convenient to use, need to install,pip install Requests , here is an example of the requests module

   #1   url  =  http://api.python.cn /api/user/stu_info   " data  = { " stu_name   :  "  small black   '   ' #请求数据req  = requests. get  (Url,params  =data) #发get请求print (Req.json ()) # The return data is a dictionary print (req.text) #返回数据是string, JSON string 

#2, send POST request
url = ' Http://api.python.cn/api/user/login '
data = {' username ': ' Test ', ' passwd ': ' aA123456 '} #请求数据
req = Requests.post (url,data) #发送post请求
Print (Req.json ())
Print (Req.text)

#3, the entry parameter is a JSON-type

Import Random
Phone=random.randint (10000000000,99999999999)
Url= ' Http://api.python.cn/api/user/add_stu '
data = {
"Name": "Small 1",
"Grade": "Scorpio",
"Phone":p Hone,
"Sex": "Male",
"Age": 28,
"Addr": "No. 32nd, Beihai Road, Jiyuan, Henan Province"
}
req = Requests.post (url,json=data)
Print (Req.json ())

# 4. Add a cookie
url = ' Http://api.python.cn/api/user/gold_add '
data = {' stu_id ': 468, ' Gold ': 10000}
DJL = {' usertest ': ' 337ca4cc825302b3a8791ac7f9dc4bc6 '}
req = Requests.post (URL,DATA,COOKIES=DJL)
Print (Req.json ())

#5, add header
url = ' Http://api.python.cn/api/user/all_stu '
Header = {
' Referer ': ' http://api.nnzhp.cn/'
}
req = Requests.get (Url,headers=header)
Print (Req.json ())

#6, uploading files
Url= ' Http://api.python.cn/api/file/file_upload '
data = {
' File ': Open (R ' C:\Users\bjniuhanyang\Desktop\ graph \6bd9026dt935575932465&690.jpg ', ' RB ')
}
req= Requests.post (Url,files=data)
Print (Req.json ())

#7, download files
url = ' Http://up.mcyt.net/?down/46779.mp3 ' #下载图片, MP3, MP4 are the same operation, find the URL of the resource to
req = requests.get (URL)
Print (req.content) #返回的是二进制模式
FW = open (' Aaa.mp3 ', ' WB ') #二进制的文件写模式是 "WB", read mode is "RB"
Fw.write (req.content)

Python Learning Notes (18) Useful module introduction in network programming

Related Article

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.