![](/img/trans.png)
[英]Python Celery socket.error: [Errno 61] Connection refused
[英]Errno 61: Connection refused error when trying to connect to python server
我最近一直在嘗試使用python的套接字模塊創建簡單的在線多人游戲。 我制作了服務器和客戶端程序的初稿,盡管當我在同一台計算機上運行它們時,它們都可以正常工作,但是當我在另一台計算機上運行時嘗試連接客戶端時,會出現以下錯誤消息:
Traceback (most recent call last):
File "/Users/Admins2-Admins_In_Space/Downloads/gameclient.py", line 22, in <module>
client.connect((host,port))
ConnectionRefusedError: [Errno 61] Connection refused
(兩台計算機都連接到同一路由器,因此這不是問題。)服務器代碼為
import socket, threading
class dataBase():
"A class to store all playerdata"
def __init__(self):
self.data = []
class client():
"handles an individual client"
def __init__(self,ip,port,value,dataBase):
self.mainThread = threading.Thread(None,self.run)
self.ip = ip
self.port = port
self.value = value
self.dataBase = dataBase
print('New connection with' + ip)
self.mainThread.start()
def run(self):
while True:
data = conn.recv(1024).decode()
if data != None:
exec('data = ' + data)
self.dataBase[self.value] = data
data = self.dataBase
message = []
for d in range(len(data)):
if d == value:
continue
message.append(data[d])
if message != []:
conn.send(str(message).encode())
else:
self.conn.close()
break
if __name__ == '__main__':
data = []
host = '127.0.0.1'
port = 1234
value = 0
threads = []
sock = socket.socket()
sock.bind((host,port))
while True:
sock.listen(5)
(conn,(ip,port)) = sock.accept()
newThread = client(ip,port,value,data)
data.append(())
threads.append(newThread)
value += 1
for t in threads:
t.join()
這是客戶,直到第22行
import pygame, socket, sys
from pygame.locals import *
host = '127.0.0.1'
port = 1234
up = False
down = False
left = False
right = False
x = 0
y = 0
data = None
if __name__ == '__main__':
pygame.init()
window = pygame.display.set_mode((1250,1000), 0, 32)
pygame.display.set_caption('client test')
client = socket.socket()
client.connect((host,port))
我一直在使用樹莓派pi 3模型b和最新版本的raspbian運行服務器,並且失敗的客戶端測試已在各種Mac上運行。
盡管當我從同一台計算機上運行它們時,它們都能正常工作,但是在另一台計算機上運行時嘗試連接客戶端會導致以下錯誤……
在客戶端中,您仍然具有host = '127.0.0.1'
(即localhost ),但是如果要連接到另一台計算機上的服務器,則必須將host
設置為該計算機的地址。 在服務器中,您應該將host = '127.0.0.1'
更改為host = '0.0.0.0'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.