【问题标题】:OSError: [Errno 99] Cannot assign requested address - pyOSError:[Errno 99] 无法分配请求的地址 - py
【发布时间】:2018-04-27 10:54:26
【问题描述】:

我刚刚为客户端/服务器示例复制了这段代码:

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

当我运行代码时出现此错误:

OSError: [Errno 99] Cannot assign requested address

服务器:

# 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.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'))

【问题讨论】:

    标签: python sockets ubuntu


    【解决方案1】:

    host = socket.gethostname() 替换为host = '127.0.0.1',它应该可以工作。

    Check this

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2021-05-26
      • 2020-09-04
      • 1970-01-01
      • 2017-09-13
      • 2020-06-06
      • 2017-05-08
      • 2018-01-02
      • 2022-11-09
      相关资源
      最近更新 更多