我正在尝试创建一个python程序,当它打开时充当客户端并检测周围任何服务器的存在,持续15秒。如果它找不到任何东西,它会将自己升级到服务器并将自己的ip广播为HOST。 iterateserver()和iterateclient()已经在外部定义。
s = socket(AF_INET, SOCK_STREAM)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
PORT = 37885
try:
s.settimeout(15)
s.connect(('', PORT))
print (HOST)
for j in range(M):
h=iterateclient()
except:
print "\n"+"Switching to server configuration"+"\n"
addr_list = [] #list of client addresses
s.bind(('', PORT))
s.listen(0)
print 'Waiting for client...'
for i in range(procno - 1): #Connects to all clients
conn, addr = s.accept(1024) #Accepts connection from client
print 'Connected by', addr
if addr not in addr_list:
addr_list.append(addr) #Adds address to address list
start_time = time.time()
for j in range(M):
h=iterateserver()
time_taken = time.time() - start_time
print 'Time taken is ', time_taken, 'seconds.'
plt.figure()
plt.plot(x2,f)
plt.show()
显示以下错误: -
$ python auto_burger.py
Switching to server configuration
Traceback (most recent call last):
File "auto_burger.py", line 34, in <module>
s.bind(('', PORT))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument