Linux 安装 request 模块

安装

yum install python-pip  -y

pip install  requests

pip3  install  requests

pip install --upgrade pip
pip3 install --upgrade pip


# 测试 requests 安装是否成功
yum install python-pip  -y

pip install  requests

pip3  install  requests

pip install --upgrade pip
pip3 install --upgrade pip

提示如下错误 就直接安装 python3

Collecting requests
  Using cached https://files.pythonhosted.org/packages/e9/23/384d9953bb968731212dc37af87cb75a885dc48e0615bd6a303577c4dc4b/requests-2.28.0.tar.gz
    Complete output from command python setup.py egg_info:
    
    ==========================
    Unsupported Python version
    ==========================
    This version of Requests requires at least Python 3.7, but
    you're trying to install it on Python 2.7. To resolve this,
    consider upgrading to a supported Python version.
    
    If you can't upgrade your Python version, you'll need to
    pin to an older version of Requests (<2.28).
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-vlVVId/requests/
You are using pip version 8.1.2, however version 22.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

安装python3

yum install python-pip  -y
yum -y install python3
pip3  install  requests

#输入python3
[root@AITest-gateway ~]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> exit()
request 安装成功

使用

# 需要我们传JSON格式的数据过去,所以我们可以用 json.dumps() 方法把表单数据序列化。
import json
import requests
url = 'http://httpbin.org/post'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload))
print(r.text)



# 如果想要上传文件,那么直接用 file 参数即可。新建一个 a.txt 的文件,内容写上 Hello World!
import requests
url = 'http://httpbin.org/post'
files = {'file': open('/root/export/test1/a.txt', 'rb')}
r = requests.post(url, files=files)
print(r.text)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值