Python Basics: Network programming

Source: Internet
Author: User

First, network programming

In short, it is the code that opens a URL to get the results back and do the processing. Often referred to as the Python crawler, it belongs to the network programming

Second, Urllib module for network programming

This method is cumbersome and is not recommended for use. Know

Example 1:

Get a Web page content, write a file

 from Import Request,parse # url = ' http://www.baidu.com ' # req = request.urlopen (URL)  #打开一个url, send GET request #  content = Req.read (). Decode () #获取返回结果, binary, Need to first decode ()#  fw = open (' baidu.html ', ' W ', encoding= ' utf-8 ')#  fw.write ( Content)

Example 2:

Gets the interface returns the result, makes the judgment

 fromUrllibImportrequest,parse# Importing the Urllib module request and parseImportJSON#url= ' http://127.0.0.1/api/user/stu_info?stu_name=aaa 'eq = request.urlopen (URL) #打开一个url, send GET request#content = Req.read (). Decode () #获取返回结果, binary, to first extract binary decode () as a string#res_dic = json.loads (content) #返回的结果转成字典#if Res_dic.get (' error_code ') = = 0:#print (' Test pass ')#Else:#print (' Test Failed ', Res_dic)

Example 3:

When parameters are passed in, use parse to stitch parameters, request URLs, get results

 fromUrllibImportRequest,parseurl='Http://api.nnzhp.cn/api/user/login'Data= {    'username':'Admin',    'passwd':'aA123456'} #request data, written in dictionary formatdata = Parse.urlencode (data)#UrlEncode, automatically spell the parameters for you.#xx=xx&xx=11req = Request.urlopen (Url,data.encode ())#send a POST requestPrint(Req.read (). Decode ())

The above see the usage of urllib first to compare trouble, step by step operation, you must first open the URL, in the incoming parameters, the results of the return to be processed.

The Requesets module has been well encapsulated in these steps. It's more convenient to use.

Third, requests

This module is part of a third-party module and requires an import installation. PIP Install requests

Example 1:

Send a GET Request URL

1, send GET request URL='Http://127.0.0.1/api/user/stu_info'Data= {'Stu_name':'AAA'}#request data, in dictionaryreq = Requests.get (url,params=data)#send a GET request, write directly to the parameter URL, and pass data to automatically access the URL, and return. Note: The GET request must be sent with the format params= parameter
#req返回的结果, is a Requests.models.Response object
Print(Req.json ())#可以把返回数据转换成DictionaryPrint(Req.text)#把返回数据转换成String,json String

Example 2:

Send a POST request

' Http://127.0.0.1/api/user/login '  = {'username':' aaa','passwd' :'123456'}  # request data, dictionary encapsulation req = requests.post (URL, Data)  # send a POST request that can pass directly to parameter data without writing paramsprint(Req.json ()) #将结果转换成字典

Example 3:

The import parameter is the JSON format

Url='Http://127.0.0.1/api/user/add_stu'Data=  {    "name":"Small 1",    "Grade":"Scorpio",    "Phone": 13640000000,    "Sex":"male",    " Age": 28,    "Addr":"Hair Stephensteffen"} #参数, JSON string req= Requests.post (url,json=data) #入参是json to specify the json= parameter when passing in the parameterPrint(Req.json ())

Example 4:

Post request with a cookie

' Http://127.0.0.1/api/user/gold_add '  = {'stu_id': 468,'gold': 10000= {'  aaa':'337ca4cc825302b3a8791ac7f9dc4bc6 '  = Requests.post (url,data,cookies=djl) #入参带有cookie, when the parameter is passed in, specify the  cookies= parameter print( Req.json ())

Example 5:

Get request with Header

' Http://127.0.0.1/api/user/all_stu '  = {    'Referer':'http://127.0.0.1/' = Requests.get (url,headers=header) #指定  headers= parameter print (Req.json ())  

Example 6:

Request to upload a file

' Http://127.0.0.1/api/file/file_upload '  = {    'file': Open (R'C:\Users\bjniuhanyang\Desktop\ Figure \6bd9026dt935575932465&690.jpg','rb')} #参数, If you upload a different file, the following RB will not be written. Because the uploaded image is saved in binary form, the read binary mode is expressed in RB. Of course, if you download a file, use wbreq= requests.post (url,files=data) #指定 files= parameter print(Req.json ())

Example 7:

Download file

' Http://up.mcyt.net/?down/46779.mp3 ' # URL of the file to downloadreq= = open ('aaa.mp3','wb' ) #mp3格式也是用二进制保存, so write binary mode with WB and read the contents of the corresponding file to Fw.write (req.content) #将读取到的内容写到一个文件中. The content of this file is the content of the downloaded file. 

Python Basics: Network programming

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.