我试图学习如何使用套接字并将一个接收ip,端口和文件的类放在一起。它可以在localhost上运行,但是当我将它传递给网络上另一台主机的ip时就不行了。
这是追溯:
var taskDiv = '<div><table border=1 class="table table-condensed">';
var progress = 47;
taskDiv += ('<tr><td>link</td><td>200min</td><td>result size</td></tr>');
taskDiv += ('<tr><td colspan="3"><div class="container"><div class="progress progress-striped active"><div class="progress-bar" style="width: 0%;"></div></div></div></td></tr>');
var $bar = $('.progress-bar');
if ( $bar.width() == 100 ) {
$( '.progress' ).removeClass( 'active' );
} else {
$bar.width(progress);
}
$bar.text(progress + "%");
}
taskDiv+=('</table></div>');
$(taskDiv).appendTo('#providers');
$providersDiv.append('</table>');
这是代码:
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()
,程序会在第一个方法的dict更新之前运行,但没有发送文件。
答案 0 :(得分:1)
对于服务器,.bind(('',port))
是典型的,意味着接受任何接口上的客户端连接。使用端口0不典型...使用数字> 1024是典型的。在客户端连接到服务器之前,代码不会超过accept
,因此在accept
停止也是正常的。