使用miniupnp通过Internet打开套接字服务器

时间:2020-09-10 12:01:34

标签: python sockets upnp

我正在尝试在库miniupnpc转发的特定端口上打开服务器套接字。问题是我无法以任何方式从其他外部设备访问服务器。这是server.py:

from miniupnpc import UPnP
import socket

upnp = UPnP()

def forward_port(port_no):
    upnp.discoverdelay = 10
    upnp.discover()
    upnp.selectigd()
    upnp.addportmapping(port_no, 'TCP', upnp.lanaddr, port_no, ' ', '')


def close_port(port_no):
    upnp.deleteportmapping(port_no, 'TCP')


PORT = 6666

try:
    forward_port(PORT)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('', PORT))
    s.listen(1)
    print(s)
    conn, addr = s.accept()
    print(addr, 'connected')
    conn.send(b'some response')
finally:
    close_port(PORT)

client.py:

import socket


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('My public ip', 6666))
print(s.recv(1024))
s.close()

服务器打印出套接字元素,但从不打印出addr。客户端出现超时错误。

0 个答案:

没有答案
相关问题