小白求教,Python3实例类调用显示没定义 8

python3text3.py--port=8800Traceback(mostrecentcalllast):File"text3.py",line15,in<modu... python3 text3.py --port=8800
Traceback (most recent call last):
File "text3.py", line 15, in <module>
class EpollServer(object):
File "text3.py", line 64, in EpollServer
server = EpollServer(host=SERVER_HOST, port=port)
NameError: name 'EpollServer' is not defined

定义了这个类,但是在主方法里实例化调用显示没定义

源码:
import socket
import select
import argparse
SERVER_HOST="localhost"
EOL1=b'\n\n'
EOL2=b'\n\r\n'
SERVER_RESPONSE=b"""HTTP/1.1 200 OK\R\nDate:Mon,1 apr 2013 01:01:01
GMT\r\nContent-Type:text/plain\r\nContent-Length:25\r\n\r\n
Hello from Epoll Srver!"""class EpollServer(object):
"""A socket server using Epoll"""
def __init__(self,host=SERVER_HOST,port=0):
self.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
self.sock.bind((host,port))
self.sock.listen(1)
self.sock.setblocking(0)
self.sock.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)
print("Started Epoll Server")
self.epoll.register(self.sock.fileno(),select.EPOLLIN)
def run(self):
"""EXecutes epoll server operation"""
try:
connections={};requests={};responses={}
while True:
events=self.epoll.poll(1)
for fileno,event in events:
if fileno==self.sock.fileno():
connection,address=self.sock.accept()
connection.setblocking(0)
self.epoll.register(connection.fileno(),select.EPOLLIN)
connections[connection.fileno()]=connection
requests[connection.fileno()]=b''
responses[connection.fileno()]=SERVER_RESPONSE
elif event & select.EPOLLIN:
requests[fileno]+=connections[fileno].recv(1024)
if EOL1 in requests[fileno] or EOL2 in requests[fileno]:
self.epoll.modify(fileno,select.EPOLLOUT)
print('_'*40+'\n'+requests[fileno].decode()[:-2])
elif event & select.EPOLLOUT:
byteswritten=connections[fileno].send(responses[fileno])
responses[fileno]=response[fileno][byteswirtten:]
if len(responses[fileno])==0:
self.epoll.modify(fileno,0)
connections[fileno].shutdown(socket.SHUT_RDWR)
elif event & select.EPOLLHUP:
self.epoll.unregister(fileno)
connections[fileno].close()
del connections[fileno]
finally:
self.epoll.unregister(self.sock.fileno())
self.epoll.close()
self.sock.close()
if __name__=='__main__':
parser=argparse.ArgumentParser(description='Socket Server Example with Epoll')
parser.add_argument('--port',action="store",dest="port",type=int,required=True)
given_args=parser.parse_args()
port=given_args.port
server = EpollServer(host=SERVER_HOST, port=port)
server.run()
展开
 我来答
阳光的雷咩咩
2019-06-21 · TA获得超过1.4万个赞
知道大有可为答主
回答量:2.3万
采纳率:66%
帮助的人:7940万
展开全部
问下你的上述类代码与main代码都在同一个文件里吗,如果在不同文件,有没有import?
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式