Home > other >  . The HTTP server running in the child are not normal, please take a look
. The HTTP server running in the child are not normal, please take a look

Time:12-19

Httpserver. HTTP server do running in the main process, into the child processes of the multiprocessing can not run normally, look at the journal found stuck in the self. Send_response (200). This way, on the run in the child process in order to convenient and put an end to this process, the following is a code
#! The/usr/bin/env python
# - coding: utf-8 --
The from multiprocessing import Process
. From the HTTP server import BaseHTTPRequestHandler, HTTPServer
The import OS
The from OS import path
The from urllib. Parse the import urlparse
The import asyncio
The import web sockets
The import of logging
The import of logging. Handlers
Import a datetime
Logger=logging. GetLogger (' mylogger ')
Logger. SetLevel (logging. The DEBUG)

Rf_handler=logging. Handlers. TimedRotatingFileHandler (' all. Log ', the when='midnight', the interval=1, backupCount=7, atTime=datetime. Time (0, 0, 0, 0))
Rf_handler. SetFormatter (logging Formatter (" % (asctime) s - % s "(the message)))

F_handler=logging. FileHandler ('. The error log)
F_handler. SetLevel (logging. The ERROR)
F_handler. SetFormatter (logging Formatter (" % (asctime) (levelname) s - s - % % (filename) s (lineno) [: % d] - % s "(the message)))

Logger. AddHandler (rf_handler)
Logger. AddHandler (f_handler)
OS. Environ [' path ']=OS. Environ [' path '] + '; '+ OS. Path. Abspath ('')

Def websocketservice () :
# receiving the client message and process, here is simply to return from the client back
Async def recv_msg (websocket) :
While True:
Recv_text=await websocket. Recv ()
Response_text=f "your submit context: {recv_text}"
Await the websocket. Send (response_text)

# main logic on the server
# websocket and path is the function callback when pass automatically, don't need to preach
Async def main_logic (websocket, path) :
Await recv_msg (websocket)

# change the IP to your local IP
The start_server=web sockets. Serve (main_logic, '127.0.0.1, 999)
# if you want to be called back main_logic pass custom parameters, can use the following forms
# a, modify callback form
# import functools
# the start_server=web sockets. Serve (functools. Partial (main_logic, other_param="test_value"), '10.10.6.91, 5678)
# modified defined by the callback function, increase the corresponding parameter
# async def main_logic (websocket, path, other_param)
Print (" webserver started ")
Asyncio. Get_event_loop (.) run_until_complete (the start_server)
Asyncio. Get_event_loop (.) run_forever ()

Def httpservice () :
Curdir=path. Dirname (path. The realpath (__file__))
Sep='/'

# MIME - TYPE
Mimedic=[
(' HTML ', 'text/HTML),
(' HTM ', 'text/HTML),
(' js', 'application/javascript),
(' CSS ', 'text/CSS),
(' json ', 'application/json),
(' PNG ', 'image/PNG'),
(' JPG 'and' image/jpeg),
(' GIF 'and' image/GIF),
(. TXT ', 'text/plain'),
Avi '(',' video/x - msvideo '),
]

The class myHTTPServer_RequestHandler (BaseHTTPRequestHandler) :
# GET
Def do_GET (self) :
SendReply=False
Querypath=urlparse (self. Path)
Filepath, query=querypath. Path, querypath. Query

If filepath. Endswith ('/') :
Filepath +='index.html'
The filename, fileext=path. Splitext (filepath)
For e in mimedic:
If e [0]==fileext:
Mimetype=e [1]
SendReply=True
If sendReply==True:
Try:
Logger. The debug (sendReply)
With the open (path. The realpath (curdir + + filepath sep), 'rb') as f:
The content=f.r ead ()
Logger. The debug (' 0 ')
Self. Send_response (# 200) this sentence, the log can print to 0, 1 can't print out
Logger. The debug (' 1 ')
Self. Send_header (' the content-type 'mimetype)
Logger. The debug (' 2 ')
Self. End_headers ()
Logger. The debug (' 3 ')
Self. Wfile. Write (content)
Logger. The debug (" 4 ")
Except IOError:
Self. Send_error (404, 'File Not Found: % s' % self. The path)
Logger. The debug (" failure ")
Port=8080
Print (' starting server, port, the port)
# Server Settings
Server_address=(' ', the port)
HTTPD=HTTPServer (server_address myHTTPServer_RequestHandler)
Logger. The debug (' running server... ')
HTTPD. Serve_forever ()

If __name__=="__main__ ':
P1=Process (target=httpservice)
P1. The daemon=True
P1. The start ()
Process of print (" 1 ")
The p2=Process (target=websocketservice)
Daemon=True p2.
P2. The start ()
Print (" process 2 ")


  • Related