A Python operating network, which is to open a Web site, or request an HTTP interface, using the Urllib module.
Urllib module is a standard module, directly import Urllib can, in Python3 inside only urllib module, in Python2 There are urllib module and URLLIB2 module.
| 12345678910111213141516171819 |
Import JSON from urllib import request from urllib import parse pay_url = ' Http://szz.nnzhp.cn/pay ' balance_url = ' http://szz.nnzhp.cn/get_balance ' balance_data = {' user_id ':1} new_balance_data = parse. UrlEncode(balance_data)#把请求参数变成xx =11. balance_req = request. Urlopen(balance_url+'? ') +new_balance_data)#发送get请求 Print(balance_req. Read(). Decode()) #获取接口返回的结果, the returned result is of type bytes, which needs to be decoded using the Decode method, into a stringpay_data ={"user_id":1,"Price":"999"} new_pay_data = parse. UrlEncode(pay_data)#把请求参数变成xx =11. pay_req = request. Urlopen(pay_url,new_pay_data. Encode())#发送post请求, incoming parameters #参数必须是bytes类型, so we need to encode first and become bytes type . Print(pay_req. Read(). Decode())#获取接口返回的结果, the result returned is the bytes type #需要使用decode方法解码, into a string res = json. Loads(pay_req. Read(). Decode()) #因为返回的是一个json传, if you want to turn the JSON string into a dictionary, use the JSON module for a long time to turn it into a dictionaryPrint(res) |
Second, the above is the use of Python urllib module to request a website, or interface, but Urllib module is too troublesome, pass parameters, all have to be bytes type, return data is 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 can, The following is an example of a requests module
| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
Import Requests pay_url = ' Http://szz.nnzhp.cn/pay ' balance_url = ' http://szz.nnzhp.cn/get_balance ' balance_data = {' user_id ':1} pay_data ={"user_id":1,"Price":"999"} balance_res = requests. Get(balance_url,balance_data). Text #发送get请求, and gets the result returned, the text gets the result of a stringPrint(balance_res) balance_res = requests. Get(balance_url,balance_data). JSON() #发送get请求, and gets the result returned, the JSON () method gets the result directly as a dictionaryPrint(balance_res) pay_res = requests. Post(pay_url,pay_data). JSON()#调用post方法 Print(pay_res) #==== entry is the = = = of the JSON stringURL = ' Http://api.nnzhp.cn/getmoney ' Data = {"userid":1} res = requests. Post(URL,json=data). JSON()#指定入参json Print(res) #====== Adding cookie===== URL = ' Http://api.nnzhp.cn/setmoney2 ' Data = {' userid ':1,' money ':9999} Cookie = {' token ':' token12345 '} res = requests. Post(URL,data,cookies=cookies). JSON()#使用cookies参数指定cookie Print(res) #===== Add permission validation =====URL = ' Http://api.nnzhp.cn/setmoney ' Data = {' userid ':1,' money ':91999} res = requests. Post(URL,data,auth=(' admin ',' 123456 ')). JSON() #使用auth参数指定权限验证的账号密码, Auth is a tuple. Print(res) #====== sending files =====URL = ' http://api.nnzhp.cn/uploadfile ' res = requests. Post(url,files={' file ':open(' api11.py ')}) . JSON() #指定files参数, pass a file, is a file object Print(res) #===== Send header======URL = ' http://api.nnzhp.cn/getuser2 ' Data = {' userid ':1} header = {' content-type ': 'application/json '} res = requests. Post(URL,headers=header). JSON() #指定headers参数, add headersPrint(res) |
Python Network programming