【问题标题】:Python - connection keeps getting refusedPython - 连接不断被拒绝
【发布时间】:2017-06-30 20:03:11
【问题描述】:

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

文件“client.py”,第 35 行,在 数据,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 上进出。

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

【问题讨论】:

  • 就像@DeepSpace 提到的那样,主机和客户端 IP 在这里很重要。如果您不是从同一台机器连接/测试,请不要绑定到 localhost。
  • 所以当 127.0.0.1 开启时,它只会接受他自己的连接吗? @DeepSpace
  • @DiogoF 是的,看看我的答案和我链接到的问题。

标签: python-3.x sockets chat lan


【解决方案1】:

在 server.py 中,将 host = '127.0.0.1' 更改为 host = '0.0.0.0'。监听127.0.0.1 将只允许来自本地主机的连接。更深入的解释见What is the difference between 0.0.0.0, 127.0.0.1 and localhost?

【讨论】:

  • 谢谢,我赶紧在google上搜索“127.0.0.1 vs 0.0.0.0”找到了解释,不知道有什么区别。
猜你喜欢
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
相关资源
最近更新 更多