[[ Простой HTTP сервер на Python ]]

Python

Простой HTTP сервер на Python

Подойдет для тестов и экспериментов

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#--------------------------------------------------------------------------------------------------
from time import sleep
from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn
 
 
class Handler(BaseHTTPRequestHandler):
    def __init__(self, *args):
        BaseHTTPRequestHandler.__init__(self, *args)
 
    def do_GET(self):
        #print(self.path)
        #print(self.headers)
        self.send_response(200)
        self.send_header('Content-Type','text/html; charset=UTF-8')
        self.end_headers()
        i = 0
        while (i < 10):
            i += 1
            self.wfile.write("{}".format(i).encode())
            self.wfile.write('\n'.encode())
            sleep(0.5)
        return
 
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    """Handle requests in a separate thread."""
 
 
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if __name__ == '__main__':
    server = ThreadedHTTPServer(('localhost', 8098), Handler)
    server.serve_forever()






Обсуждение

Ваш комментарий. Вики-синтаксис разрешён:
152 +9 = 
 
handmade/python/python_http_server.txt · Последние изменения: 2019/03/18 10:00 — lexa
Gentoo Linux Gentoo Linux Driven by DokuWiki