Python使用HTTP代码示例模版

以下是一个使用Python发送HTTP请求的示例代码模板:

```python

import requests

# 发送GET请求

def send_get_request(url, params=None, headers=None):

response = requests.get(url, params=params, headers=headers)

return response

# 发送POST请求

def send_post_request(url, data=None, headers=None):

response = requests.post(url, data=data, headers=headers)

return response

# 示例代码

if __name__ == '__main__':

# 发送GET请求示例

get_url = 'https://api.example.com/get'

get_params = {'param1': 'value1', 'param2': 'value2'}

get_headers = {'User-Agent': 'Mozilla/5.0'}

get_response = send_get_request(get_url, params=get_params, headers=get_headers)

print(get_response.status_code)

print(get_response.text)

# 发送POST请求示例

post_url = 'https://api.example.com/post'

post_data = {'key1': 'value1', 'key2': 'value2'}

post_headers = {'User-Agent': 'Mozilla/5.0'}

post_response = send_post_request(post_url, data=post_data, headers=post_headers)

print(post_response.status_code)

print(post_response.text)

```

以上代码使用了Python的`requests`库来发送HTTP请求。你可以根据需要修改URL、参数、请求头等内容。

#! -*- 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

你可能感兴趣的:(python,爬虫,前端)