python - 是否有关于使用 python 进行端口转发的简单描述?

标签 python sockets networking port

我可以在同一台机器上使用

进行简单的套接字通信

服务器:

import socket

s = socket.socket()
host = socket.socket()
port = 8000
s.bind((host,port))
s.listen(5)
while true:
     c,addr = s.accept()
     print 'got connection from', addr
     c.send('thank you for connecting')
     c.close()

客户:

import socket 

s = socket.socket()
host=socket.gethostname()
port = 8000
s.connect((host,port))
print s.recv(1024)

要让我的笔记本电脑和我工作的私有(private)服务器之间进行通信,需要进行哪些更改?我从搜索中发现,端口转发是实现它的最佳方式,但没有找到任何关于如何实现它的解释或教程。

谢谢

最佳答案

如果你真的不需要在 python 中执行此操作,只需使用 netcat: -

http://netcat.sourceforge.net/

端口转发或端口映射 在 Linux 上,NetCat 可用于端口转发。以下是在 NetCat 中进行端口转发的九种不同方法(尽管不支持 -c 开关 - 这些方法与 netcat 的“ncat”化身一起使用):

nc -l -p port1 -c ' nc -l -p port2'
nc -l -p port1 -c ' nc host2 port2'
nc -l -p port1 -c ' nc -u -l -p port2'
nc -l -p port1 -c ' nc -u host2 port2'
nc host1 port1 -c ' nc host2 port2'
nc host1 port1 -c ' nc -u -l -p port2'
nc host1 port1 -c ' nc -u host2 port2'
nc -u -l -p port1 -c ' nc -u -l -p port2'
nc -u -l -p port1 -c ' nc -u host2 port2'

来源:- http://en.wikipedia.org/wiki/Netcat#Port_Forwarding_or_Port_Mapping

它通常作为大多数 *nix 发行版的标准配置,并且还有一个 Win32 端口:-

http://www.stuartaxon.com/2008/05/22/netcat-in-windows/

关于python - 是否有关于使用 python 进行端口转发的简单描述?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11350145/

相关文章:

python - Pandas 中的代码标准化和应用功能花费了太多时间

具有更直观上限的 Python for 循环?

python - 在 Apache2 上为 django 部署配置 mod_wsgi 后出现 404 错误

c++ - 如何在Qt中创建线程网络服务器?

Android 模拟器未连接到互联网

ruby - Vagrant:在 private_network 中设置 VM 的 MAC 地址(hostonly)

Python 属性错误 : module 'serial' has no attribute 'Serial'

LoadRunner可以通过UDP包接收数据吗?

java - 套接字和 Java 聊天

java - 网络中的对象编写