研究了一下发现这些基本都不能定源端口,不过我找GPT写了一个脚本,自己改了一下,实现一个简单的本地反代
[Python] 纯文本查看 复制代码 import http.server
import http.client
class ReverseProxyHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
# 目标主机
target_host = '127.0.0.1'
# 目标端口
target_port = 8080
source_address = ('127.0.0.1', 55555) # 这里就是固定发起请求的端口了
target_connection = http.client.HTTPConnection(target_host, target_port, source_address = source_address)
target_connection.request('GET', self.path)
target_response = target_connection.getresponse()
# 转发
self.send_response(target_response.status)
for header in target_response.getheaders():
self.send_header(header[0], header[1])
self.end_headers()
self.wfile.write(target_response.read())
if __name__ == '__main__':
# 监听本地端口
server_address = ('127.0.0.1', 60000)
httpd = http.server.HTTPServer(server_address, ReverseProxyHandler)
httpd.serve_forever()
实测浏览器访问 localhost:60000,抓包发现55555端口和8080通信,然后60000把结果返回给浏览器。
|