以下是一個(gè)使用Python發(fā)送HTTP請(qǐng)求的示例代碼模板:
```python
import requests
# 發(fā)送GET請(qǐng)求
def send_get_request(url, params=None, headers=None):
response = requests.get(url, params=params, headers=headers)
return response
# 發(fā)送POST請(qǐng)求
def send_post_request(url, data=None, headers=None):
response =?requests.post(url, data=data, headers=headers)
return response
# 示例代碼
if __name__ == '__main__':
# 發(fā)送GET請(qǐng)求示例
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)
# 發(fā)送POST請(qǐng)求示例
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)
```文章來源:http://www.zghlxwxcb.cn/news/detail-509467.html
以上代碼使用了Python的`requests`庫來發(fā)送HTTP請(qǐng)求。你可以根據(jù)需要修改URL、參數(shù)、請(qǐng)求頭等內(nèi)容。文章來源地址http://www.zghlxwxcb.cn/news/detail-509467.html
#! -*- encoding:utf-8 -*-
import requests
# 要訪問的目標(biāo)頁面
targetUrl = "http://ip.hahado.cn/ip"
# 代理服務(wù)器
proxyHost = "ip.hahado.cn"
proxyPort = "39010"
# 代理隧道驗(yàn)證信息
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
到了這里,關(guān)于Python使用HTTP代碼示例模版的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!