如何在Python中完全退出服务器?类似server.shutdown()的代码只是关闭传入的请求。有人能为完全退出服务器提供任何代码吗?这是一个简单的socketserver。在
这是我的服务器代码:#!/usr/bin/python
import socket
from threading import Thread
class sockServer:
def __init__(self,port):
self.socket= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(port)
self.socket.listen(5)
def _run(self):
while True:
request, client_address =self.socket.accept()
counter=self.req_thread(self,request)
counter.start()
class req_thread(Thread):
def __init__(self,controller,request):
Thread.__init__(self)
self.controller=controller
self.request=request
self.setDaemon(True)
def run(self):
input=self.request.makefile('rb',0)
output=self.request.makefile('wb',0)
l=True
i=0
try:
while l:
l=input.readline()
if l!="exit\r\n":
print "x"
output.write(bytes('hello\n'))
else:
print "y"
self.request.shutdown(2)
run=False
except socket.error:
pass
if __name__ == '__main__':
import sys
if len(sys.argv) < 3:
print('Usage: %s [hostname] [port number]' % sys.argv[0])
sys.exit(1)
hostname = sys.argv[1]
port = int(sys.argv[2])
global run
run=True
while run:
server=sockServer((hostname,port))
server._run()
使用变量run的方法没有任何好处!在