This article mainly introduces the method and steps of using Python to create a simple HTTP server. The sample code in the article is very detailed. It has a certain reference learning value for everyone's study or work. Friends who need it, let's learn together.
The following is a sample code to send an HTTP request using Python:
1. Send a GET request
```python
import requests
response = requests.get('Example Domain')
print(response.status_code) # print response status code
print(response.text) # print response content
```
2. Send a POST request
```python
import requests
data = {'username': 'example', 'password': '123456'}
response = requests.post('Example Domain', data=data)
print(response.status_code) # print response status code
print(response.text) # print response content
```
3. Send the request with the request header
```python
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get('Example Domain', headers=headers)
print(response.status_code) # print response status code
print(response.text) # print response content
```
4. Send a request with parameters
```python
import requests
params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get('Example Domain', params=params)
print(response.status_code) # print response status code
print(response.text) # print response content
```
The above are some common HTTP request examples, you can modify and expand according to your needs.
#! -*- encoding:utf-8 -*-
import requests
# 要访问的目标页面
targetUrl = "http://ip.hahado.cn/ip"
# 代理服务器
proxyHost = "ip.hahado.cn"
proxyPort = "39010"
# 代理隧道验证信息
proxyUser = "username"
proxyPass = "password"
proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
"host" : proxyHost,
"port" : proxyPort,
"user" : proxyUser,
"pass" : proxyPass,
}
proxies = {
"http" : proxyMeta,
"https" : proxyMeta,
}
resp = requests.get(targetUrl, proxies=proxies)
print resp.status_code
print resp.text