Python Server Client程序错误:"OSError:[WinError 10048]"; [英] Python Server Client program error: "OSError: [WinError 10048]"

查看:664
本文介绍了Python Server Client程序错误:"OSError:[WinError 10048]";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在从Kenneth Lambert撰写的《 Python的基础知识》一书中学习Python,但我遇到书中其中一个程序错误的麻烦.

So I'm studying Python from the book Fundamentals of Python by Kenneth Lambert and I'm having trouble with an error from one of the programs in the book.

在第10章中讨论了客户端和服务器.我的教授要求我们在Python中键入这些程序的键,以查看它们如何工作.最初的程序运行良好,但是在程序中出现了一个错误,似乎是Windows错误而不是Python错误.

Here in chapter 10 talks about Clients and Servers. My professor asked us to key those programs in Python to see how they work. The first programs worked perfectly, but in a program I'm getting an error which seems to be a Windows error instead of a Python one.

这是第339页中的程序:

This is the program in page 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()

当我运行该程序时,它将在命令行管理程序中显示等待连接"消息.但是,当我尝试使用命令提示符连接到该程序时,它显示以下错误:

When I run this program, it displays the "Waiting for connection" message in the Shell. However, when I try to connect to the program using the Command Prompt, it displays the following error:

C:\Python33>python multi-client-server.py
Traceback (most recent call last):
  File "multi-client-server.py", line 30, in <module>
    server.bind(ADDRESS)
OSError: [WinError 10048] Only one usage of each socket address (protocol/networ
k address/port) is normally permitted

我们在课堂上没有对此进行很多研究.所以我只是想知道为什么会发生这种情况以及如何解决它.

We haven't studied this in class a lot. So I'm just wondering why this happens and how to fix it.

谢谢!

推荐答案

因此,根据您的问题:

我们在课堂上没有对此进行很多研究.所以我想知道为什么 发生以及如何解决.

We haven't studied this in class a lot. So I'm just wondering why this happens and how to fix it.

原因: 您正在尝试在Windows OS上从两个不同的CMD运行相同的代码段.因此,当您最初执行代码段时,服务器开始在port number 5000上侦听,然后当您从第二个CMD窗口执行相同的代码段时,它与第一个CMD窗口已使用的套接字发生冲突. 我在Windows 8上对此进行了测试.

Why: You are trying to run the same code snippet from two different CMD on a Windows OS. So, when you initially execute the code snippet, the server starts listening on the port number 5000, then when you execute the same code snippet from the second CMD wndow it conflicts with the socket that is already being used by the first one. I tested this on Windows 8.

如何解决: 要解决此问题,您必须在第二次执行代码片段时简单地使用不同的端口号,以使套接字(IP + port)与前一个不冲突.只需编辑您的代码并放入PORT = 15200并使用其他名称保存此文件.(我也提供了下面的代码.)现在尝试从CMD窗口中执行第一个代码段文件,然后执行您想要的第二个代码段文件.现在从第二个CMD窗口创建.该问题将得到解决!

How to fix: To fix this issue, you have to simply use a different port number when you execute the code snippet for the second time, so that the socket(IP+port) doesn't conflicts with the previous one. Simply edit your code and put PORT = 15200 and save this file with a different name.(I have provided the code below too.) Now try executing the first code snippet file from a CMD windows and then execute the second code snippet file that you created right now from the second CMD window. The issue will be solved!

代码:

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 = 15200 # Port number was changed here
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()

如果您愿意,请在这里查看基本的客户端-服务器问题.

If you prefer then have a look here for the basic client-server issues.

这篇关于Python Server Client程序错误:"OSError:[WinError 10048]";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆