0
我想学习如何使用套接字和放在一起,需要一个IP,端口和文件发送的类。它在本地主机上工作,但当我通过网络上的另一主机的IP时不能工作。发送文件通过套接字 - 绑定()
这是回溯:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/path/to/anaconda3/envs/env/lib/python3.5/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "script.py", line 91, in servr
servr.bind((self.ip, self.port))
OSError: [Errno 99] Cannot assign requested address
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "script.py", line 163, in <module>
main()
File "script.py", line 157, in main
rmt.client()
File "script.py", line 85, in client
machines.update({self.ip: {self.port: path.get()}})
File "/path/to/anaconda3/envs/env/lib/python3.5/multiprocessing/pool.py", line 608, in get
raise self._value
OSError: [Errno 99] Cannot assign requested address
这是代码:
class Remote:
def __init__(self, ip, port, filename):
self.ip = socket.gethostbyname(ip)
self.port = int(port)
self.filename = filename
def client(self):
pool = Pool(processes=1)
path = pool.apply_async(self.servr)
time.sleep(1)
client = socket.socket()
client.connect((self.ip, self.port))
with open(self.filename, 'rb') as file:
data = file.read()
client.sendall(data)
machines = {}
try:
with open("machines.pickle", 'rb') as file:
machines = pickle.load(file)
except (EOFError, FileNotFoundError):
pass
finally:
with open("machines.pickle", 'wb') as file:
machines.update({self.ip: {self.port: path.get()}})
pickle.dump(machines, file)
def servr(self):
servr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
servr.bind((self.ip, self.port))
servr.listen(5)
client, addr = servr.accept()
file = open("." + self.filename, 'wb') if platform.system() == 'Linux' else os.popen(
"attrib +h " + self.filename, 'wb')
data = client.recv(6000)
file.write(data)
file.close()
file = "." + self.filename if platform.system() == 'Linux' else self.filename
os.chmod(file, os.stat(file).st_mode | 0o111)
client.close()
servr.close()
return os.path.abspath(file)
我试图结合("", 0)
但随后的代码不过去的呼叫运行accept()
。也许这个端口已经被使用了?我也试过socket.settimeout()
,虽然它突破accept()
,程序运行,直到第一种方法的字典更新,但没有文件发送。