扭曲的Python服务器端口已在使用中 [英] twisted python server port already in use

查看:31
本文介绍了扭曲的Python服务器端口已在使用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(顺便说一句,使用Mac)

我正在学习this构建扭曲的python套接字服务器的教程,一切都很顺利。

我面临的一个问题是我不知道如何关闭服务器。基本上,我更改了我的python脚本中的一些代码,我想重启服务器,但我不知道如何重启。我尝试从我的活动监视器中终止所有的python进程,但是当我再次尝试运行服务器时,我得到一个错误,即服务器无法在端口80上侦听。

脚本如下:

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor

class IphoneChat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
        print "clients are ", self.factory.clients

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

    def dataReceived(self, data):
        a = data.split(':')
        print a
        if len(a) > 1:
            command = a[0]
            content = a[1]

            msg = ""
            if command == "iam":
                self.name = content
                msg = self.name + " has joined"

            elif command == "msg":
                msg = self.name + ": " + content
                print msg

            for c in self.factory.clients:
                c.message(msg)
    def message(self, message):
        self.transport.write(message + '
')

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(80, factory)
print "Iphone Chat server started"
reactor.run()
回溯(最近一次调用): 文件"pythonSocketServer.py",第39行,位于 Reacter.listenTCP(80,出厂) 文件"/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/posixbase.py",行495,在listenTcp中 P.startListing() 文件"/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/tcp.py",第980行,在开始侦听中 引发CannotListenError(self.interface,self.port,le) Twisted.interet.error.CannotListenError:无法侦听已在使用的任何:80:[Errno 48]地址。

推荐答案

使用netstat -nlp | grep 80查找使用端口80的进程。

如果可能,请使用kill -9 pid终止进程。

或者您可以使用其他端口,如12345。

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(12345, factory)

这篇关于扭曲的Python服务器端口已在使用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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