本文整理汇总了Python中server.Server.serve_forever方法的典型用法代码示例。如果您正苦于以下问题:Python Server.serve_forever方法的具体用法?Python Server.serve_forever怎么用?Python Server.serve_forever使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类server.Server
的用法示例。
在下文中一共展示了Server.serve_forever方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: serve_forever
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import serve_forever [as 别名]
def serve_forever(self, poll_interval=0.5):
# make sure every process knows the https parent pid
secure_pid = os.getpid()
plain_pid = os.fork() if self.plain_mode else None
# when using simultaneous plain http, fork and run that part on the child.
# os.fork() warns about ssl and multiprocessing. I think we're okay tho,
# since only the parent does any ssl and its right on the socket???
# plain (child) part:
if plain_pid == 0:
# TODO: catch signals from / poll for pid of parent?
try:
self.plain_server.serve_forever(poll_interval)
finally:
# could probably investigate the nature of our pid more closely,
# however pidfiles are too tryhard atm
if secure_pid == os.getppid():
os.kill(secure_pid, signal.SIGTERM)
# secure (parent) part:
else:
# might get away w/ lazy style? if more sophisticated management is desired
# can override SocketServer.py copypastastyle
try:
Server.serve_forever(self, poll_interval)
finally:
# again, might want to think at least two seconds about our cleanup...
# None == no fork even happened.
if plain_pid is not None:
os.kill(plain_pid, signal.SIGTERM)
示例2: start_servers
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import serve_forever [as 别名]
def start_servers():
coord = Coordinator(('', 10000))
coord.serve_forever()
srv1 = Server(('', 10001))
srv1.serve_forever()
srv2 = Server(('', 10002))
srv2.serve_forever()
示例3: main
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import serve_forever [as 别名]
def main():
parser = ArgumentParser()
parser.add_argument("-p", "--port", required=True)
parser.add_argument("-f", "--file_dir", required=True)
args = parser.parse_args()
port = int(args.port)
file_dir = args.file_dir
server = Server(("", port), RequestHandler, file_dir)
print "Starting server on port: " + str(port)
server.serve_forever()
示例4: main
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import serve_forever [as 别名]
def main():
parser = ArgumentParser()
parser.add_argument('-p', '--port', required=True)
#parser.add_argument('-f', '--file_dir', required=True)
args = parser.parse_args()
port = int(args.port)
#file_dir = args.file_dir
file_dir = ''
server = Server(('',port), RequestHandler, file_dir)
print 'Starting server on port: ' + str(port)
server.serve_forever()
示例5: test_serve
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import serve_forever [as 别名]
def test_serve(self):
sock_mock = Mock()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
t = ('127.0.0.1',5568)
attrs = {'socket.return_value':sock_mock, 'accept.side_effect':[(s,t),(s,t),KeyboardInterrupt],'connect.side_effect':[None, OSError]}
sock_mock.configure_mock(**attrs)
with patch('server.socket',sock_mock):
srv = Server()
srv.run_proxies = MagicMock(return_value = None)
try:
srv.serve_forever()
except KeyboardInterrupt:
pass
sock_mock.connect.assert_called_with(('localhost', 7979))
示例6: ServerThread
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import serve_forever [as 别名]
class ServerThread(threading.Thread):
def __init__(self,port,logic):
threading.Thread.__init__(self)
self.startserver(port,logic)
def startserver(self,port,logic):
try:
self.server = Server(("127.0.0.1",port),logic)
self.closing = 0
except:
self.startserver(port+2,logic)
def run(self):
try:
self.server.serve_forever()
except:
if(self.closing == 0):
self.run()
def stop(self):
r = 2
示例7: do
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import serve_forever [as 别名]
desc = "Sums two integers and returns their sum."
def do(self):
return self.num1 + self.num2
class FnAdd2(Function):
from_version = 1
extname = "add"
params = [("num1", exttype.ExtInteger, "Number 1"),
("num2", exttype.ExtInteger, "Number 2"),
("num3", exttype.ExtInteger, "Number 3")]
returns = (exttype.ExtInteger, "Sum of num1, num2 and num3")
desc = "Sums three integers and returns their sum."
def do(self):
return self.num1 + self.num2 + self.num3
class MyServer(Server):
authenticator = authentication.NullAuthenticationManager
srv = Server("venus.ita.chalmers.se", 12121)
srv.register_function(FnAdd)
srv.register_function(FnAdd2)
srv.serve_forever()
示例8: SSLServer
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import serve_forever [as 别名]
class SSLServer(Server):
def __init__(self,
listen_secure,
handler,
cert_path,
key_path,
plain_mode = 0,
listen_plain = None,
handler_plain = None
):
# figure out if we need to expose an http fallback
try:
if plain_mode in PLAIN.values:
self.plain_mode = plain_mode
else:
self.plain_mode = getattr(PLAIN, str(plain_mode).upper())
except:
raise Exception("unrecognized mode '{0}'".format(repr(plain_mode)))
Server.__init__(self, listen_secure, handler)
#print listen_secure
#print self.socket
# the magic that allows simple https connections
self.socket = ssl.wrap_socket(
self.socket,
certfile=cert_path,
keyfile=key_path,
server_side=True
)
# e.g. if any https is running
if self.plain_mode:
if listen_plain is None:
raise Exception("need to specify `listen_plain` arg when running dual http+https")
# CUSTOM plain mode requires a seperate, explicit handler
if self.plain_mode == PLAIN.CUSTOM:
if handler_plain is None:
raise Exception("need to specify `handler_plain` when a separate handler is desired for http")
# use the same handler for http and https
elif self.plain_mode == PLAIN.MIRROR:
handler_plain = handler
# redirect all http requests to http asap
elif self.plain_mode == PLAIN.REDIRECT:
handler_plain = SSLRedirectHandler
else:
raise Exception("shouldn't be able to get this far without a valid plain_mode???")
self.plain_server = Server(listen_plain, handler_plain)
else:
self.plain_server = None
def serve_forever(self, poll_interval=0.5):
# make sure every process knows the https parent pid
secure_pid = os.getpid()
plain_pid = os.fork() if self.plain_mode else None
# when using simultaneous plain http, fork and run that part on the child.
# os.fork() warns about ssl and multiprocessing. I think we're okay tho,
# since only the parent does any ssl and its right on the socket???
# plain (child) part:
if plain_pid == 0:
# TODO: catch signals from / poll for pid of parent?
try:
self.plain_server.serve_forever(poll_interval)
finally:
# could probably investigate the nature of our pid more closely,
# however pidfiles are too tryhard atm
if secure_pid == os.getppid():
os.kill(secure_pid, signal.SIGTERM)
# secure (parent) part:
else:
# might get away w/ lazy style? if more sophisticated management is desired
# can override SocketServer.py copypastastyle
try:
Server.serve_forever(self, poll_interval)
finally:
# again, might want to think at least two seconds about our cleanup...
# None == no fork even happened.
if plain_pid is not None:
os.kill(plain_pid, signal.SIGTERM)