• python基础七--网络编程


    requests 模块,可以获取网页内容;接口返回的json;向网站发起请求(get、post、cookie、header、上传文件等)。

    import requests  #requests模块就是基于urllib模块开发的,所有requests模块更便宜操作
    
    # requests.get(url).text  #text方式返回的是字符串
    # res = requests.get(url).json()  #返回的json(dict或list)
    
    #get请求
    url_login = 'http://127.0.0.1:5000/login?username=admin&password=123456'
    res = requests.post(url_login).json()
    print(type(res),res)
    
    #post请求
    url_set = 'http://127.0.0.1:5000/set_sties'
    d = {
        "stie":"牛牛杂货铺",
        "url":"http://www.nnzhp.cn/blog/"
    }
    res = requests.post(url_set,json=d).json()
    print(res)
    
    #添加cookie
    cookie_url = 'http://127.0.0.1:5000/set_cookies'
    data = {'username':'晓晓',"money":22222}
    cookie = {'token':"admin-login"}
    res = requests.post(cookie_url,data=data,cookies=cookie).json()
    print(res)
    
    #添加header
    head_url = 'http://127.0.0.1:5000/set_header'
    data = {'userid': 1}
    header = {'Content-Type': "application/json"}
    res = requests.post(head_url, headers=header).json()
    
    #上传文件
    up_url = 'http://127.0.0.1:5000/upload'
    file = {'file_name':open('test.py')}
    res = requests.post(up_url,files=file).text
    print(res)
  • 相关阅读:
    Linux 下安装JDK1.8
    Linux 常规操作
    C3P0连接池拒绝连接
    Oracle查看并修改最大连接数
    Oracle 建立 DBLINK
    Oracle 数据 update后怎么恢复到以前的数据
    Oracle 11g中解锁被锁定的用户
    身份证15位转18位
    Druid数据库连接池
    CentOS 下安装 LEMP 服务(Nginx、MariaDB/MySQL 和PHP)
  • 原文地址:https://www.cnblogs.com/eeoo/p/7132827.html
Copyright © 2020-2023  润新知