如何在局域网中使用两台不同的计算机制作客户端 - 服务器?

问题描述

它对我来说只是超时,即使我使用 gethostbyname 方法或 gethostname 作为私有 IP 地址....

代码

def connect(self):
        nm = nmap.PortScanner()
        nm.scan(hosts = '192.168.1.0/24',arguments = '-n -sP -PE -PA21,23,80,3389')
        devices = []
        hosts_list = [(x,nm[x]['status']['state']) for x in nm.all_hosts()]
        for ip,state in hosts_list:
            devices.append(ip)
        for ip in devices:
            print(ip,type(ip))
            
            try:
                client_socket = socket.socket()
                client_socket.settimeout(2)
                try:
                    client_socket.connect((ip,self.port))
                    if type(client_socket) != None:
                        client_socket.send('hi'.encode())
                        client_socket.settimeout(None)
                        return client_socket
                except Exception as e:
                    print(e)
            except socket.error:
                pass

当它到达正确的 Ip 时,它只是......超时。以为我的功能有问题,但事实并非如此。

TLDR-使用Nmap做了一个局域网自动连接,它唯一连接的是超时和我的悲伤D:

编辑:让我告诉你我运行这个函数时发生的错误

服务器:

    server_socket = socket.socket()
    hostname = socket.gethostname()
    local_ip = socket.gethostbyname(hostname)
    server_socket.bind((local_ip,self.port))

双方决定他们想要使用的端口,我使用socket.gethostbyname作为服务器ip。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)