端口转发不适用于python套接字

端口转发不适用于python套接字,python,python-2.7,sockets,portforwarding,Python,Python 2.7,Sockets,Portforwarding,我现在正在学习如何使用python套接字,现在我正在尝试使用我创建的简单聊天程序与我的一位朋友聊天。当然,程序不起作用,因为我没有转发它 我使用在whatismyip.com找到的ip(ip是静态的)作为我的外部ip转发了端口21342,而cmd中ipconfig命令的输出中显示为ipv4的ip作为内部ip 现在,即使在转发之后,它仍然不起作用。我是否仍然缺少一些明显的东西,或者这是一个真正的问题 代码: 服务器: import socket import threading as thread

我现在正在学习如何使用python套接字,现在我正在尝试使用我创建的简单聊天程序与我的一位朋友聊天。当然,程序不起作用,因为我没有转发它

我使用在
whatismyip.com
找到的ip(ip是静态的)作为我的外部ip转发了端口21342,而
cmd
ipconfig
命令的输出中显示为ipv4的ip作为内部ip

现在,即使在转发之后,它仍然不起作用。我是否仍然缺少一些明显的东西,或者这是一个真正的问题

代码:

服务器:

import socket
import threading as thread

sock = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
host = ''
sock.bind((host , 21342))
sock.listen(5)
connections = []
def chat(client , address):
    global connections
    while True:
        info = client.recv(1024)
        for connection in connections:
            if connection != client:
                connection.send(info)
        if not info:
            connections.remove(client)
            client.close()
            break
try:
    while True:
        client , address = sock.accept()
        print 'new connection!'
        client_thread = thread.Thread(target = chat , args = (client , address))
        client_thread.daemon = True
        client_thread.start()
        connections.append(client)
    sock.close()
except:
    for connection in connections:
        connection.close()
    sock.close()
客户:

import socket
import threading as thread

sock = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
host = 'ip found at whatsmyip.com'
sock.connect((host , 21342))
def get_info(sock):
    while True:
        print sock.recv(1024)
get_info_thread = thread.Thread(target = get_info , args = [sock])
get_info_thread.daemon = True
get_info_thread.start()
while True:
    sock.send(raw_input())
sock.close()

您是否禁用了防火墙(如果有)?如果在同一台机器上运行server.py和client.py呢?@user1034937是的。我应该把它添加到帖子中吗?@user1034937如果在同一台机器上它像一个符咒一样工作,“不工作”不是有用的错误描述。预期的行为是什么?会发生什么?请在你的问题中详细说明你收到的任何错误信息(不要作为评论)。@SteffenUllrich正是这样。我收到的唯一错误消息是,当您输入错误的套接字编号或在不打开服务器的情况下打开客户端时,通常会出现无法连接的消息