端口转发不使用python套接字

时间:2018-06-12 10:31:27

标签: python python-2.7 sockets portforwarding

我正在学习如何使用python套接字,现在我正在尝试使用我创建的简单聊天程序与我的一个朋友交谈。当然,porgram没有用,因为我没有移植它。

我使用ip(ip是静态的)转发了端口21342我在whatismyip.com找到了我的外部ip,ip在ipconfig命令的oputput中显示为ip { {1}}作为内部ip。

现在,即使转发它仍然无法正常工作。我仍然遗漏了一些明显的东西,或者这是一个真正的问题吗?

代码:

服务器:

cmd

客户端:

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()

0 个答案:

没有答案