Python http
时间: 2023-12-03 13:05:55 浏览: 61
以下是Python中使用requests库进行HTTP请求的例子:
1. 发送GET请求并获取响应内容
```python
import requests
url = 'http://www.example.com'
response = requests.get(url)
print(response.text)
```
2. 发送POST请求并获取响应内容
```python
import requests
url = 'http://www.example.com'
data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post(url, data=data)
print(response.text)
```
3. 上传文件
```python
import requests
url = 'http://123.56.99.53:9001/api/uploadFile/'
headers = {"Cookie": "uid=1;token=44c972f05d76fdd93c31f9c2b65bb098f308cdfc"}
files = {"myfile1": open('D:\全力以富\1.docx', 'rb')}
response = requests.post(url, headers=headers, files=files)
print(response.text)
```
4. 发送带账号名密码的POST请求
```python
import requests
import base64
url = 'http://123.56.99.53:9000/event/api/admin/'
headers = {"Content-Type": "application/x-www-form-urlencoded"}
username = 'admin'
password = '123admin'.encode('utf-8')
password = base64.b64encode(password).decode('utf-8')
data = {"username": username, "password": password}
response = requests.post(url, headers=headers, data=data)
print(response.text)
```
阅读全文