首页 > 解决方案 > 在python中连接服务器和客户端时出现超时错误

问题描述

我在服务器端使用了这个:

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.bind((socket.gethostname(),9999))
mysock.listen(1)
while True:
    clt, addr = mysock.accept()
    print('Connection request from', addr, 'was successfully established')
    clt.send('Connected to XYZ'.encode())
    clt.send('Closing connection, byee'.encode())
    clt.close()

这在客户端:

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(("my ip",9999))        #will put ip of server

while True:
    msg = mysock.recv(1024)
    if len(msg) < 1:
        break
    mystring = msg.decode()
    print(mystring)

在这里,当客户端脚本运行时,它会产生以下错误:

PS C:\Users\91829\Blogo> & C:/Users/91829/AppData/Local/Programs/Python/Python37/python.exe c:/Users/91829/Desktop/uu.py
Traceback (most recent call last):
  File "c:/Users/91829/Desktop/uu.py", line 4, in <module>
    mysock.connect(("my ip",9999))
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
PS C:\Users\91829\Blogo>

注意:在此服务器和主机是不同的机器。服务器是本地主机,即我的机器,客户端是我朋友的机器

谢谢你的帮助。

标签: pythonsocketsnetworkingwebsocketpycharm

解决方案


推荐阅读