zoukankan      html  css  js  c++  java
  • pythone函数基础(13)发送网络请求

    需要导入urllib模块,request模块
    发送网络请求有两种方法

    第一种方法
    # from urllib.request import urlopen
    # from urllib.parse import urlencode
    #
    # # url='http://127.0.0.1:8999/api/login?username=testuser1&passwd=111111'
    # # res = urlopen(url).read()
    # # print(res.decode())
    # url='http://127.0.0.1:8999/api/login'
    # data = {'username':'testuser1','passwd':'111111'}
    # s = urlencode(data)
    # res = urlopen(url,s.encode()) #post请求
    # print(res.read().decode())
    第二种方法
    import requests
    import random
    url='http://127.0.0.1:8999/api/upload'
    # data = {'username':'testuser1','passwd':'111111'}
    # r = requests.get(url,params=data) #发get请求
    # r = requests.post(url,data=data) #发post请求

    # data = {
    # "session_id":"6ab8785039dcf50fb11c53acc1db7648",
    # "name":"zhouyongbo%s"%random.randint(1,99),
    # "phone":"111211345%02d"%random.randint(1,99),
    # "grade":"英语座"
    # }
    # #
    # r = requests.post(url,json=data) #入参是json类型的

    #上传文件的
    # r = requests.post(url,
    # data={'session_id':'6ab8785039dcf50fb11c53acc1db7648'},
    # files={'file_name':open('account.xls','rb') }
    # )

    # requests.get(url,headers={'cookie':'pt2gguin=o0511402865; RK=JQZpwBp1by; ptcz=6c30e26a9ed6be93d3de9e4c4aca3e55650cf99fcffa64729bd1d58a5fb209d9; pgv_pvi=779236352; pgv_pvid=6970909788; qb_qua=; qb_guid=818de686e29d412fa4ee9e99905ea166; Q-H5-GUID=818de686e29d412fa4ee9e99905ea166; NetType=; pgv_si=s4499960832; FTN5K=0138ca95; pgv_info=ssid=s4269564370; luin=o0511402865; uin=o0511402865; lskey=00010000efc2701412d3429029ac9366e4ba98f0e978e0ae4a9c684101a7b22df0695f534bc242c8d4ff386d; skey=@0sHtvhTsD; ptisp=cnc; p_uin=o0511402865; pt4_token=wGU2YAaM0uu7LbcNjuDcLN-TPrEy7AZw4gcL5TEcKxw_; p_skey=1zg7yvF5wr6l43mfr-BvNHxuVDtybKpR5RbCrgC8weQ_'})


    # requests.get(url,cookies={'pt2ggui':'o0511402865','RK':'JQZpwBp1by'})
    # print(r.text) #结果返回的就是字符串
    # print(r.json()) #结果返回的就是字典,必须返回的是json,才能转成字典
    r.content#是用来接收图片歌曲等文件的


    #下载文件、上传文件、添加header

    #下载文件
    # url='https://q4.qlogo.cn/g?b=qq&nk=1834364415&s=140'

    # url='https://qiniuuwmp3.changba.com/1127063572.mp4'
    r = requests.get(url)
    #返回的就是二进制的
    r.cookies #返回cookie
    r.status_code #返回的状态码200


    f = open('sdfsdf.mp4','wb')
    f.write(r.content)
    f.close()
  • 相关阅读:
    Visual Studio LightSwitch
    Android 虚拟机与真机调试配置
    点击手机 menu 硬件按钮后的显示及处理
    Windows Phone 7 真机调试
    Android 调试
    Android 新建项目 页面
    今天我的Windows Phone 7 HTC HD7 手机 升级 NoDo 了 分享一下经验
    Activity 之间调用与参数传递
    Android widget 组件
    解决 warning: found plain 'id' attribute; did you mean the new 'android:id' name? 问题
  • 原文地址:https://www.cnblogs.com/yulinlincoding/p/10280728.html
Copyright © 2011-2022 走看看