66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
# -*- encoding:utf-8 -*-
|
||
|
||
'''
|
||
@Author : dingjiawen
|
||
@Date : 2023/12/13 19:13
|
||
@Usage :
|
||
@Desc : request相关代理的设置
|
||
'''
|
||
|
||
import requests
|
||
|
||
|
||
def http_demo():
|
||
proxy = '127.0.0.1:7890'
|
||
proxies = {
|
||
'http': 'http://' + proxy,
|
||
'https': 'http://' + proxy,
|
||
}
|
||
try:
|
||
# 传入proxies参数即可
|
||
response = requests.get('https://httpbin.org/get', proxies=proxies)
|
||
print(response.text)
|
||
except requests.exceptions.ConnectionError as e:
|
||
print('Error', e.args)
|
||
|
||
|
||
def http_auth_demo():
|
||
proxy = 'username:password@127.0.0.1:7890'
|
||
proxies = {
|
||
'http': 'http://' + proxy,
|
||
'https': 'http://' + proxy,
|
||
}
|
||
try:
|
||
# 传入proxies参数即可
|
||
response = requests.get('https://httpbin.org/get', proxies=proxies)
|
||
print(response.text)
|
||
except requests.exceptions.ConnectionError as e:
|
||
print('Error', e.args)
|
||
|
||
|
||
# 如果代理模式是socks:需要额外安装pip install requests[socks]
|
||
def socks():
|
||
proxy = '127.0.0.1:7891'
|
||
proxies = {
|
||
'http': 'socks5://' + proxy,
|
||
'https': 'socks5://' + proxy
|
||
}
|
||
try:
|
||
response = requests.get('https://httpbin.org/get', proxies=proxies)
|
||
print(response.text)
|
||
except requests.exceptions.ConnectionError as e:
|
||
print('Error', e.args)
|
||
|
||
|
||
# 另一种socks方式,此方法属于全局配置
|
||
def socks2():
|
||
import socks
|
||
import socket
|
||
socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 7891)
|
||
socket.socket = socks.socksocket
|
||
try:
|
||
response = requests.get('https://httpbin.org/get')
|
||
print(response.text)
|
||
except requests.exceptions.ConnectionError as e:
|
||
print('Error', e.args)
|