OSError: [Errno 99] Cannot assign requested address - py

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

I just copied this code for a client/server example:

http://www.bogotobogo.com/python/python_network_programming_server_client.php

and when I run the code I get this error:

OSError: [Errno 99] Cannot assign requested address 

Server:

# server.py  import socket                                          import time  # create a socket object serversocket = socket.socket(             socket.AF_INET, socket.SOCK_STREAM)   # get local machine name host = socket.gethostname()                             port = 9999                                             # bind to the port serversocket.bind((host, port))                                    # queue up to 5 requests serversocket.listen(5)                                             while True:     # establish a connection     clientsocket,addr = serversocket.accept()            print("Got a connection from %s" % str(addr))     currentTime = time.ctime(time.time()) + "\r\n"     clientsocket.send(currentTime.encode('ascii'))     clientsocket.close() 

Client:

# client.py   import socket  # create a socket object s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   # get local machine name host = socket.gethostname()                             port = 9999  # connection to hostname on the port. s.connect((host, port))                                 # Receive no more than 1024 bytes tm = s.recv(1024)                                       s.close()  print("The time got from the server is %s" % tm.decode('ascii')) 

回答1:

Replace host = socket.gethostname() with host = '127.0.0.1' and it should work.

Check this

Hope this wil help :)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!