在Python中,你可以使用`requests`库来发送HTTP请求。以下是一些示例代码:
**发送GET请求:**
```python
import requests
url = "https://www.example.com"
response = requests.get(url)
print(response.text) # 输出响应内容
```
在这个例子中,我们使用`requests.get`方法向指定的URL发送GET请求,并打印出响应的内容¹。
**发送POST请求:**
```python
import requests
url = "http://localhost:5000/post"
data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post(url, json=data)
print(response.text) # 输出响应内容
```
在这个例子中,我们使用`requests.post`方法向指定的URL发送POST请求。我们将要发送的数据存储在一个字典中,并将其作为JSON数据发送³。
**使用HTTP代理:**
```python
import requests
proxy = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",
}
response = requests.get("http://www.example.com", proxies=proxy)
print(response.text) # 输出响应内容
```
在这个例子中,我们使用了`requests`库来发送HTTP请求,并设置了代理服务器⁵。
这只是一些基本的示例,你可以根据你的具体需求来修改或扩展这些代码。如果你有任何问题,欢迎随时向我提问。😊
源: 与必应的对话, 2023/11/6
(1) Python requests 模块 | 菜鸟教程. https://www.runoob.com/python3/python-requests.html.
(2) 用Python发送post请求简单实例-CSDN博客. https://blog.csdn.net/scx_qsnl/article/details/89788483.
(3) Python使用HTTP代理代码 - 知乎 - 知乎专栏. https://zhuanlan.zhihu.com/p/635364638.
(4) python发送http请求_Plusroax的博客-CSDN博客. https://blog.csdn.net/ad72182009/article/details/116161689.
(5) Python 发送 HTTP请求包终极指南:从入门到精通 - 知乎. https://zhuanlan.zhihu.com/p/659975239.
(6) undefined. https://www.runoob.com/.
(7) undefined. https://www.runoob.com/try/ajax/json_demo.json.
(8) undefined. https://www.runoob.com/?s=python+%E6%95%99%E7%A8%8B.
(9) undefined. http://www.tuling123.com/openapi/api.
(10) undefined. http://httpbin.org/post.