Python - 连接一直被拒绝

时间:2017-06-30 20:03:11

标签: python-3.x sockets chat lan

我正在使用python创建一个小型局域网聊天,由于某种原因,我一直拒绝连接。这是错误:

  

文件“client.py”,第35行,in       data,addr = s.recvfrom(1024)ConnectionRefusedError:[Errno 111]拒绝连接

这是server.py代码:

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()

我的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)

server.py正在我的RPi上运行,他是我的 ufw status verbose 输出:

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)

client.py ufw设置几乎相同,我允许进出端口5000。

我做错了什么?如果您对代码有任何建议,请告诉我们!

1 个答案:

答案 0 :(得分:2)

在server.py中,将#define Open / ## * #define Close * ## / 更改为host = '127.0.0.1'。在host = '0.0.0.0'上侦听只允许来自localhost的连接。有关更深入的解释,请参阅What is the difference between 0.0.0.0, 127.0.0.1 and localhost?