Proxy

设置代理后测试的网址是:httpbin.org/get,访问该网址可以得到请求的相关信息,其中origin宇段就是客户端的IP,可以根据它来判断代理是否设置成功,即是否成功伪装了IP。

urllib

HTTP代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from urllib.request import ProxyHandler, build_opener
from urllib.error import URLError

proxy = "127.0.0.1:1087"
proxy_handler = ProxyHandler({
'http': "http://" + proxy,
'https': "https://" + proxy
})
opener = build_opener(proxy_handler)

try:
response = opener.open("http://httpbin.org/get")
print(response.read().decode('utf-8'))
except URLError as e:
print(e.reason)

# {
# "args": {},
# "headers": {
# "Accept-Encoding": "identity",
# "Connection": "close",
# "Host": "httpbin.org",
# "User-Agent": "Python-urllib/3.7"
# },
# "origin": "38.141.44.98",
# "url": "http://httpbin.org/get"
# }

SOCKS5代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from urllib import request
from urllib.error import URLError
import socket
import socks

socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 1086)
socket.socket = socks.socksocket

try:
response = request.urlopen("http://httpbin.org/get")
print(response.read().decode('utf-8'))
except URLError as e:
print(e.reason)

# {
# "args": {},
# "headers": {
# "Accept-Encoding": "identity",
# "Connection": "close",
# "Host": "httpbin.org",
# "User-Agent": "Python-urllib/3.7"
# },
# "origin": "38.141.44.98",
# "url": "http://httpbin.org/get"
# }

requests

HTTP代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import requests

proxy = "127.0.0.1:1087"
proxies = {
'http': "http://" + proxy,
'https': "https://" + proxy
}

try:
response = requests.get("http://httpbin.org/get", proxies=proxies)
print(response.text)
except requests.ConnectionError as e:
print("Error", e.args)

# {
# "args": {},
# "headers": {
# "Accept": "*/*",
# "Accept-Encoding": "gzip, deflate",
# "Connection": "close",
# "Host": "httpbin.org",
# "User-Agent": "python-requests/2.19.1"
# },
# "origin": "38.141.44.98",
# "url": "http://httpbin.org/get"
# }

SOCKS5代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import requests

proxy = "127.0.0.1:1086"
proxies = {
'http': "socks5://" + proxy,
'https': "socks5://" + proxy
}

try:
response = requests.get("http://httpbin.org/get", proxies=proxies)
print(response.text)
except requests.ConnectionError as e:
print("Error", e.args)

# {
# "args": {},
# "headers": {
# "Accept": "*/*",
# "Accept-Encoding": "gzip, deflate",
# "Connection": "close",
# "Host": "httpbin.org",
# "User-Agent": "python-requests/2.19.1"
# },
# "origin": "38.141.44.98",
# "url": "http://httpbin.org/get"
# }

Selenium

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from selenium import webdriver

proxy = "127.0.0.1:1087"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server=http://" + proxy)
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get("http://httpbin.org/get")

# {
# "args": {},
# "headers": {
# "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
# "Accept-Encoding": "gzip, deflate",
# "Accept-Language": "en-us",
# "Connection": "close",
# "Cookie": "aaa=111; undefined=undefined; _gauges_unique=1; _gauges_unique_month=1; _gauges_unique_year=1",
# "Dnt": "1",
# "Host": "httpbin.org",
# "Upgrade-Insecure-Requests": "1",
# "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15"
# },
# "origin": "218.82.103.201",
# "url": "http://httpbin.org/get"
# }