Python Proxy


Pyhon Proxy

1. python利用http/https代理访问外网

#!/usr/bin/python3
import requests
from googletrans import Translator

proxy = '127.0.0.1:8118'
proxies = {
    'http': 'http://' + proxy,
    'https': 'https://' + proxy,
}
try:
    response = requests.get('http://google.com.hk', proxies=proxies)
    print(response.text)
except requests.exceptions.ConnectionError as e:
    print('Error', e.args)

python利用sock5代理访问外网

#!/usr/bin/python3
import requests
import socks
import socket

socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 12345)
socket.socket = socks.socksocket
try:
    response = requests.get('http://google.com.hk')
    print(response.content)
except requests.exceptions.ConnectionError as e:
    print('Error', e.args)
#!/usr/bin/python3
from requests_html import HTMLSession

session = HTMLSession()
proxy = {"http": "socks5://127.0.0.1:12345","https": "socks5://127.0.0.1:12345"}
url = "http://www.google.com"
req = session.get(url, proxies=proxy)
print(req.text)