commit 8d83b126087b01434426cf0fa55a61d695fdb656 Author: MasonLiu <2857911564@qq.com> Date: Fri Nov 29 16:31:32 2024 +0800 first commit diff --git a/PyProxy.py b/PyProxy.py new file mode 100644 index 0000000..18452a5 --- /dev/null +++ b/PyProxy.py @@ -0,0 +1,41 @@ +# 基础示范例子,后续加入轮转代理 +import socket +import socks +import threading +from socketserver import ThreadingMixIn, TCPServer, StreamRequestHandler + +class ThreadingTCPServer(ThreadingMixIn, TCPServer): + pass + +class SocksProxyHandler(StreamRequestHandler): + def handle(self): + # 获取客户端请求的地址和端口 + remote_address = self.rfile.readline().strip().decode('utf-8').split(':') + remote_host = remote_address[0] + remote_port = int(remote_address[1]) + + # 设置SOCKS代理 + socks.set_default_proxy(socks.SOCKS5, 'localhost', 1080) + socket.socket = socks.socksocket + + # 连接到远程主机 + try: + with socket.create_connection((remote_host, remote_port)) as remote_socket: + # 将客户端数据转发到远程主机 + while True: + data = self.request.recv(4096) + if not data: + break + remote_socket.sendall(data) + response = remote_socket.recv(4096) + self.request.sendall(response) + except Exception as e: + print(f"Error: {e}") + +def start_proxy_server(host='0.0.0.0', port=8888): + server = ThreadingTCPServer((host, port), SocksProxyHandler) + print(f"Starting proxy server on {host}:{port}") + server.serve_forever() + +if __name__ == '__main__': + start_proxy_server() \ No newline at end of file diff --git a/example.py b/example.py new file mode 100644 index 0000000..a779ee4 --- /dev/null +++ b/example.py @@ -0,0 +1,15 @@ +import requests + +def main(): + # 配置代理 + proxies = { + 'http': 'socks5://localhost:8888', + 'https': 'socks5://localhost:8888' + } + + # 发送HTTP请求 + response = requests.get('http://example.com', proxies=proxies) + print(response.text) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8cd1e30 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +PySocks +requests \ No newline at end of file