gpt4 book ai didi

Error connecting to a global Python Socket server(连接到全局Python套接字服务器时出错)

转载 作者:bug小助手 更新时间:2023-10-25 21:40:30 24 4
gpt4 key购买 nike



Decided to study what is a server and how to create it.With the first point coped but with its creation there was a problem.For the test decided to create a chat for 2 users (me and my friend). At me on the PC all works well when my friend threw the file that would test there was an error text which I will post below.

我决定研究什么是服务器以及如何创建它。第一点解决了问题,但它的创建有一个问题。测试决定为两个用户(我和我的朋友)创建一个聊天。在我的PC上,当我的朋友抛出将测试有错误文本的文件时,一切都运行得很好,我将在下面发布。


Here is how server.py looks like

以下是server.py的外观


import socket

HOST = socket.gethostbyname(socket.gethostname())
PORT = 65432
print(HOST)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)

Here is how client.py looks like

以下是client.py的外观


import socket

HOST = "тут ip которое я получил таким путём socket.gethostbyname(socket.gethostname()) себя на пк"
PORT = 65432

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b"Hello, world")
data = s.recv(1024)

print(f"Received {data!r}")

Error that occurred in client.py on a friend's PC

朋友PC上的client.py中出现错误


File "C:\Users\Username\Downloads\client.py", line 7, in s.connect((HOST, PORT)) TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

文件“C:\USERS\USERNAME\DOWNLOADS\CLIENT.PY”,第7行,位于s.Connect((主机,端口))超时错误:[WinError 10060]连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机没有响应


tried to look up the answer to the question on the Internet

我试着在网上查找这个问题的答案


更多回答
优秀答案推荐

Possible problem #1: a firewall does not allow an incoming connection to that port.

可能的问题#1:防火墙不允许到该端口的传入连接。


Possible problem #2: you are trying to connect over Internet. It will not work due to a mechanism called NAT. It is difficult - often impossible - to open a connection to an ISP client, because clients do not have a valid public address. I don't want to repeat the known facts about decades long IPv4 address shortage, NAT, private addresses, etc. Please continue your search on Internet for details if you want.

可能出现的问题#2:您正在尝试通过Internet连接。由于一种名为NAT的机制,它将无法工作。由于客户端没有有效的公有地址,因此很难--通常是不可能--打开到ISP客户端的连接。我不想重复有关长达数十年的IPv4地址短缺、NAT、私有地址等已知事实。如果您愿意,请继续在Internet上搜索详细信息。


更多回答

> HOST = socket.gethostbyname(socket.gethostname()) -- On most cases this will resolve in to 127.0.0.1 and if the client is not explicitly using HOST='127.0.0.1' or its running on different machine - NAT or not, combination will not work..

>host=socket.gethostbyname(socket.gethostname())--在大多数情况下,这将解析为127.0.0.1,如果客户端没有显式使用host=‘127.0.0.1’或其在不同计算机上运行-NAT与否,组合将不起作用。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com