如何在python中使用socket连接到远程电脑?

问题描述 投票:0回答:1

我正在尝试使用 IPv6 连接两台远程 PC 上的 python 套接字程序。我没有使用 IPv4,因为我知道由于我的路由器使用 NAT,我的外部和内部 IP 会有所不同。我认为因为我可见的临时 IPv6 (whatismyipaddress.com) 与我的系统显示的临时 IPv6 (ipconfig /all) 相同,所以两个套接字都可以轻松通过互联网连接。但服务器仍然没有收到客户端的任何连接请求。我已将防火墙配置为不阻止代码。我找不到有关 IPv6 的更多信息。

服务器代码-

import socket

s = socket.create_server(('myTempIPv6', 50000), family = socket.AF_INET6)

connection, address = s.accept()

print("Conncted with the client\nWaiting for Client's msg...")


while True:
    try:
        print("Client's msg: " + connection.recv(1024).decode())
    except ConnectionResetError:
        print('Disconnected from client!')
        break

    msg = input("Your msg: ")
    connection.sendall(msg.encode())

客户端代码-

import socket

connection = socket.create_connection(('myTempIPv6', 50000))

print('Conncted with the server')

while True:
    msg = input("Your msg: ")

    try:
        connection.sendall(msg.encode())
    except ConnectionResetError:
        print('Connection closed by the server!')
        break

    print("Server's msg: " + connection.recv(1024).decode())

我已经确认他们在局域网上工作,但在互联网上没有反应,请帮助!!

python python-3.x sockets networking ipv6
1个回答
0
投票

您必须打开服务器电脑与 WAN 或 WIFI 网络设备的连接,并使其可发现。然后,您必须输入您的公共 IP,并启用以下端口并启用端口转发

© www.soinside.com 2019 - 2024. All rights reserved.