1. 程式人生 > 其它 >使用本地sha dow socks代理

使用本地sha dow socks代理

技術標籤:# Python 常用

使用urllib2

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()

使用urllib

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())

使用request

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)