Im Erstellen eines kleinen LAN-Chat mit Python und aus irgendeinem Grund bekomme ich immer wieder meine Verbindung verweigert. Hier ist der Fehler:Python - Verbindung wird immer verweigert
File "client.py", line 35, in data, addr = s.recvfrom(1024) ConnectionRefusedError: [Errno 111] Connection refused
Hier wird der server.py Code:
import socket
from time import sleep
host = '127.0.0.1'
port = 5000
ips = []
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
print('server started')
quitS = False
while not quitS:
data, addr = s.recvfrom(1024)
if 'quitS' in str(data):
print('server will close in...')
for i in reversed(range(4)):
sleep(1)
print (i)
quitS = True
break
print (str(addr) + ': '+str(data))
if addr not in ips:
ips.append(addr)
for ip in ips:
s.sendto(data, ip)
s.close()
Und mein client.py:
import socket
from time import sleep
from getpass import getpass
host = '192.168.1.126'
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((host, port))
loop = True
while loop:
try:
s.settimeout(4)
text = input('Type: ')
data = text.encode('UTF-8')
if text == 'quitS':
passwd = False
pcount = 0
while not passwd:
pcount += 1
pwd = getpass()
if pwd == '1234':
s.send(data)
passwd = True
elif pcount == 3:
print ('HHell no, go away')
break
elif text == 'q':
s.close()
break
elif text == '':
print('Inavalid, entry not allowed.')
else:
s.send(data)
data, addr = s.recvfrom(1024)
print (str(addr) + ': ' + str(data))
except (socket.timeout, ConnectionResetError):
loop = False
sleep(2)
print('Server is dead, will close in...')
for i in reversed(range(4)):
sleep(1)
print (i)
Die server.py auf meinem RPi läuft und sein ist mein ufw status wortreiche ausgang:
5000 ALLOW IN Anywhere
6001 ALLOW IN Anywhere
5001 ALLOW IN Anywhere
22 ALLOW IN Anywhere
5900 ALLOW IN Anywhere
5800 ALLOW IN Anywhere
5000 ALLOW IN Anywhere (v6)
6001 ALLOW IN Anywhere (v6)
5001 ALLOW IN Anywhere (v6)
22 ALLOW IN Anywhere (v6)
5900 ALLOW IN Anywhere (v6)
5800 ALLOW IN Anywhere (v6)
5000 ALLOW OUT Anywhere
5000 ALLOW OUT Anywhere (v6)
Das client.py UFW-Setup ist ziemlich gleich, ich habe auf Port 5000 rein und rausgelassen.
Was mache ich falsch? Und wenn Sie Vorschläge für den Code haben, lassen Sie es mich wissen!
Wie @DeepSpace Erwähnungen, die Host-und Client-IPs sind hier wichtig. Wenn Sie nicht von demselben Computer aus eine Verbindung herstellen/testen, binden Sie nicht an localhost. – Peri461
also, wenn 127.0.0.1 eingeschaltet ist, werden nur Verbindungen von ihm selbst akzeptiert? @DeepSpace – DiogoF
@DiogoF Ja, siehe meine Antwort und die Frage, die ich verlinkt habe. – DeepSpace