网络编程
网络编程
from urllib.request import urlopen from urllib.parse import urlencode url='http://ip/api/user/stu_info' # res = urlopen(url)#发送get请求 # print(res.read().decode()) data = {'username':'niuhanyang','passwd':'aA123456'} # res= urlopen(url,urlencode(data).encode())#post请求 # print(res.read().decode()) #k=v&x=1 import requests # res = requests.get(url,params={'stu_name':'小黑'})#发送get请求 # print(res.json()) #.json直接把返回结果转成字典 # url2='http://ip/api/user/login' # data = {'username':'niuhanyang','passwd':'aA123456'} # res = requests.post(url2,data=data) # print(res.json()) # url2='http://ip/api/user/add_stu' # data = { # "name":"requests_name", # "grade":"天蝎座", # "phone":17712532946, # "sex":"男", # "age":28, # "addr":"河南省济源市北海大道32号" # } # res = requests.post(url2,json=data)#入参是json类型的 # print(res.json()) # url3 = 'http://ip/api/user/gold_add' # data = {'stu_id':15,'gold':200} # cookie = {'niuhanyang':'abd9a0995f4696e1a60133220b32037a'} # res = requests.post(url3,data=data,cookies=cookie) # print(res.json()) # url4='http://ip/api/user/all_stu' # header = {'Referer':'http://api.nnzhp.cn/'} # res=requests.get(url4,headers=header) # print(res.json())#返回的是字典 # url5='http://www.nnzhp.cn' # res = requests.get(url5) # print(res.json())#返回的是字典 # print(res.text) #返回的都是字符串 # url6='https://aliimg.changba.com/cache/photo/855e5493-f018-44db-8892-c8660649327b_640_640.jpg' # res = requests.get(url6,verify=False)#verify=False如果是https的话加上这个 # print(res.text) # with open('tu.jpg','wb') as fw: # fw.write(res.content) # print(res.json()) #必须返回的是json才可以用 # print(res.text) #下载文件的话text就不行了 # print(res.content) #用来下载文件文件用的,返回的是二进制 # print(res.headers)#获取到返回的所有header # print(res.cookies)#获取到返回的所有cookie # # url6 = 'http://ip/api/file/fisdfsd' # data = {'file':open('魔鬼中的天使.mp3','rb')} # res = requests.post(url6,files=data) # print(res.json()) # print(res.status_code) #获取它的状态码