requests代理样例
requests代理样例
1 request
import requests
if __name__ == '__main__':
url = "http://www.baidu.com"
proxies = {
'http': "127.0.0.1:8080",
'https': "127.0.0.1:8080",
}
response = requests.post(url, proxies=proxies)
print(response.status_code)
2 request.session
import requests
if __name__ == '__main__':
session = requests.session()
proxies = {'http': '127.0.0.1:8080', 'https': '127.0.0.1:8080'}
session.proxies = proxies
right_url = "http://www.baidu.com"
wrong_url = "https://www.baidu.com"
response = session.get(right_url)
print(response.status_code)
3 http https
import requests
if __name__ == '__main__':
session = requests.session()
proxies = {'http': '127.0.0.1:8080', 'https': '127.0.0.1:8080'}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate',
'Content-Type': 'application/x-www-form-urlencoded',
'Upgrade-Insecure-Requests': '1',
'Host': 'http://www.baidu.com',
}
# 不使用https
http_url = "http://www.baidu.com"
response = requests.get(http_url, headers=headers, proxies=proxies)
# 使用https,此时使用verify会报错,因为没有burpsuite的证书,将verify设置为False后会提示不安全,但不会报错
# https_url = "https://www.baidu.com"
# response = requests.get(https_url, headers=headers, proxies=proxies)
# response = requests.get(https_url, headers=headers, proxies=proxies, verify=False)
# 可以发现不管是否使用https在访问的时候都返回403,因为百度会重定向https,但是我们又没有burpsuite的证书,所以不安全403
print(response.status_code)
print(response.text)
requests CA证书:https://requests.readthedocs.io/zh_CN/latest/user/advanced.html#ssl
一个较好的讨论:https://www.v2ex.com/t/436240
如果需要用socket代理,则需先安装
pip install pysocks
import requests
if __name__ == '__main__':
url = "http://www.baidu.com"
proxies = {
'http': "socks5://127.0.0.1:8080",
'https': "socks5://127.0.0.1:8080",
}
response = requests.post(url, proxies=proxies)
print(response.status_code)