基于socke和线程的python http服务器

python version: 3.7.3

import socket
import threading


class Worker(threading.Thread):
    def __init__(self, sock):
        threading.Thread.__init__(self)
        self.sock = sock

    def run(self):
        while True:
            conn, addr = self.sock.accept()
            conn.recv(1024)
            print(addr)
            conn.send(bytes('HTTP/1.0 200 OK\n\n

hi, there!

'
, encoding='utf8')) conn.close() sock = socket.socket() sock.bind(('127.0.0.1', 1231)) sock.listen(1) for i in range(3): w = Worker(sock) w.start()

你可能感兴趣的:(Python)