Python 中的套接字编程引发错误 socket。error:< [errno 10060] 连接尝试失败



我是套接字编程的新手。我已经实现了通过互联网传输文件的客户端和服务器脚本。这两个脚本都在局域网计算机上工作。我尝试将局域网中的文件从1MB交换为1GB,脚本运行良好。

但是当我使用这两个脚本在不同的网络上工作时,意味着互联网。它不起作用。我已经检查了两个系统的防火墙规则。允许使用Python。这是代码片段。有人可以告诉我问题是什么吗?

Script -> Server.py
import os
import socket
file_path = r'C:UsersBabyDownloads06.jpg'
file_size = str(os.path.getsize(file_path))
file_name = file_path.split('\')[-1]
# print file_name.encode('UTF-8')
total = file_name+ '/'+file_size
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
# value of host = '103.235.165.205' as this is my ip which i am using in client.py
s.bind((host,21000))
s.listen(5)
print 'Server is listening on {}:21000'.format(host)
conn,addr = s.accept()
print 'Got connection from IP :{} Host :{}'.format(addr[0],addr[1])
conn.send(total.encode())
with open(file_path,'rb') as data:
while data:
conn.send((data.read()))
break
conn.close()
print 'transfer complete'
s.close()

Script -> Client.py
import socket
s = socket.socket()
s.connect(('10.174.238.205',21000))
total = (s.recv(1024)).decode()
file_name , file_size = map(str,total.split('/'))
data = s.recv(int(file_size.decode()))
with open(file_name,'wb') as getfile:
getfile.write(data)

服务器中的每个网络适配器都应与其自己的 iP 地址相关联。

s.bind(( 命令应使用与要侦听连接器的网络适配器关联的 IP 或主机名。 这应该是连接到运行客户端的网络的适配器。

此外,如果连接在本地网络之外,则必须使用公开可见的 IP。 你可以在这里和许多其他地方阅读有关IP分配的信息(谷歌是你的朋友(。 通常,公共 IP 地址将由您的 ISP 分配(有时需要额外付费,因为它们是一种稀缺资源。

感谢@jasonharper在评论中指出公共与私有 IP 问题。

相关内容

最新更新