Home >Backend Development >Python Tutorial >Based on the proxy instance in the python requests library

Based on the proxy instance in the python requests library

不言
不言Original
2018-05-07 15:08:232283browse

This article mainly introduces the proxy examples based on the python requests library, which has certain reference value. Now I share it with you. Friends in need can refer to it

directly Above code:

#request代理(proxy)
"""
1.启动代理服务器Heroku,相当于aliyun
2.在主机1080端口启动Socks 服务
3.将请求转发到1080端口
4.获取相应资源
首先要安装包pip install 'requests[socksv5]'
"""

import requests
#定义一个代理服务器,所有的http及https都走socks5的协议,sock5相当于http协议,它是在会话层
#把它转到本机的1080端口
proxies={'http':'socks5://127.0.0.1:1080','https':'socks5:/127.0.0.1.1080'}
url='https://www.facebook.com'
#下面这样访问是会报错,因为没有用代理服务器,直接访问有防火墙
response=requests.get(url,timeout=10)
response=requests.get(url,proxies=proxies,timeout=10)
print response.status_code

Related recommendations:

Correct way to open log based on Python

Methods in the decoration class based on Python decorator

Detailed explanation of array array and matrix matrix based on Python Numpy_python

The above is the detailed content of Based on the proxy instance in the python requests library. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn