因此,我从Kenneth Lambert的《Python基本原理》一书中学习了Python,我遇到了来自本书中某个程序的错误。
在第10章中,我们将讨论客户机和服务器。我的教授让我们用Python编写这些程序,看看它们是如何工作的。第一个程序运行得很好,但是在一个程序中,我得到了一个错误,这个错误似乎是Windows错误,而不是Python错误。
这是339页的程序:from socket import *
from time import ctime
from threading import Thread
class ClientHandler(Thread):
"""Handles a client request."""
def __init__(self, client):
Thread.__init__(self)
self._client = client
def run(self):
self._client.send(bytes(ctime() + '\nHave a nice day!' , 'ascii'))
self._client.close()
HOST = "localhost"
PORT = 5000
BUFSIZE = 1024
ADDRESS = (HOST, PORT)
server = socket(AF_INET, SOCK_STREAM)
server.bind(ADDRESS)
server.listen(5)
# The server now just waits for connections from clients
# and hands sockets off to client handlers
while True:
print('Waiting for connection')
client, address = server.accept()
print('...connected from:', address)
handler = ClientHandler(client)
handler.start()
当我运行这个程序时,它会在Shell中显示“Waiting for connection”消息。但是,当我尝试使用命令提示符连接到程序时,它会显示以下错误:C:\Python33>python multi-client-server.py
Traceback (most recent call last):
File "multi-client-server.py", line 30, in
server.bind(ADDRESS)
OSError: [WinError 10048] Only one usage of each socket address (protocol/networ
k address/port) is normally permitted
我们在课堂上没怎么学过这个。所以我想知道为什么会发生这种情况以及如何解决。
谢谢!