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()
,程序運行,直到第一種方法的字典更新,但沒有文件發送。