安装
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)