广州制作网站平台,阳东城乡规划建设局网站,做论坛网站的应用,wordpress 表单 ajax背景
有很多程序是在虚拟机上运行的#xff0c;返回的url 又是127.0.0.1。在个人电脑上调试需要解决这个问题。端口转发是一个不错的方法
可能的解决办法#xff1a;
1.修改程序#xff0c;返回虚拟机的ip #xff08;要改代码#xff0c;换虚拟机还要再改代码#xf…背景
有很多程序是在虚拟机上运行的返回的url 又是127.0.0.1。在个人电脑上调试需要解决这个问题。端口转发是一个不错的方法
可能的解决办法
1.修改程序返回虚拟机的ip 要改代码换虚拟机还要再改代码
2.在中间加个nginx需要额外部署服务
3.在个人电脑上用本文的脚本快速开启一个端口转发。把访问127.0.0.1的流量转发到虚拟机上本文
本文方法的优势
1.快速转发的目的ip和端口方便修改
2.代码公开安全干净
3.只依赖python3 无python三方包依赖
4.windows linux MAC 通用国产系统国产CPU也能用
本文的不足
1.需要安装python3
2.本脚本只实现了tcp未实现udp
正文
依赖python3
请在电脑上提前安装好python3
适用范围
WindowsMAClinux都通用
运行此脚本快速实现端口转发
执行如下命令即可实现
python 本脚本.py
或
python3 本脚本.py脚本正文
import socket
import threading# 转发数据的函数
def forward_data(source, destination):while True:try:data source.recv(4096) # 从源读取数据if not data:breakdestination.sendall(data) # 将数据发送到目标except Exception as e:print(fError forwarding data: {e})break# 处理客户端连接的函数
def handle_client(client_socket, target_host, target_port):try:# 连接到目标地址target_socket socket.create_connection((target_host, target_port))print(fConnected to target {target_host}:{target_port})# 创建两个线程分别转发数据thread1 threading.Thread(targetforward_data, args(client_socket, target_socket))thread2 threading.Thread(targetforward_data, args(target_socket, client_socket))thread1.start()thread2.start()# 等待线程结束thread1.join()thread2.join()except Exception as e:print(fConnection error: {e})finally:client_socket.close()target_socket.close()print(Connection closed)# 主程序
def start_proxy(listen_port, target_host, target_port):server socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 也可改为 127.0.0.1server.bind((0.0.0.0, listen_port))server.listen(5)print(fListening on port {listen_port} and forwarding to {target_host}:{target_port})while True:client_socket, addr server.accept()print(fAccepted connection from {addr})client_handler threading.Thread(targethandle_client, args(client_socket, target_host, target_port))client_handler.start()if __name__ __main__:# 本地127.0.01监听的端口TCPLISTEN_PORT 8090# 目标地址和目标端口TCPTARGET_HOST 10.9.0.8TARGET_PORT 8081start_proxy(LISTEN_PORT, TARGET_HOST, TARGET_PORT) 文章转载自: http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.xfrqf.cn.gov.cn.xfrqf.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.wcyr.cn.gov.cn.wcyr.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.dlrsjc.com.gov.cn.dlrsjc.com http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.pcjw.cn.gov.cn.pcjw.cn http://www.morning.wpmlp.cn.gov.cn.wpmlp.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.jxltk.cn.gov.cn.jxltk.cn http://www.morning.etsaf.com.gov.cn.etsaf.com http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.qdxkn.cn.gov.cn.qdxkn.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.rfxw.cn.gov.cn.rfxw.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.htbbp.cn.gov.cn.htbbp.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.fssjw.cn.gov.cn.fssjw.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.wcjgg.cn.gov.cn.wcjgg.cn http://www.morning.dongyinet.cn.gov.cn.dongyinet.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.rsfp.cn.gov.cn.rsfp.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.smcfk.cn.gov.cn.smcfk.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.xrwtk.cn.gov.cn.xrwtk.cn http://www.morning.gswfs.cn.gov.cn.gswfs.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.ntffl.cn.gov.cn.ntffl.cn http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn