使用本地shadow socks代理

1,第一种方式
import urllib2
import socks
from sockshandler import SocksiPyHandler

opener = urllib2.build_opener(SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 1080))
x = opener.open("http://www.youtube.com/")
print x.read()
2,第二种方式
from urllib import request

proxies = {
    'https': 'https://127.0.0.1:1080',
    'http': 'http://127.0.0.1:1080'
}
# 需要加上headers, 否则报错: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 8974: invalid start byte

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}

google_url = 'https://www.google.com'
opener = request.build_opener(request.ProxyHandler(proxies))
request.install_opener(opener)

req = request.Request(google_url, headers=headers)
response = request.urlopen(req)

print(response.read().decode())
3,第三种方式
import requests

response = requests.get("http://www.google.com", proxies={
    'http': 'http://127.0.0.1:1080',
    'https': 'https://127.0.0.1:1080'
})

print(response.text)

猜你喜欢

转载自www.cnblogs.com/lvchengda/p/12618023.html