[Python-14] Programación de red-Modelo HTTP


# 基于tcp创建http服务器

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host_name = socket.gethostname()
host = socket.gethostbyname(host_name)

print(host)
s.bind((host, 8088))

s.listen(5)

while True:
    conn, addr = s.accept()

    # 请求处理 | 得到客户端的请求信息,请求的路径,请求的方法 ... 我们在通过这些信息做出不同的响应
    request = conn.recv(1024) 

    # 响应处理
    response = b'''HTTP/1.x 200 OK
    Content-Type: text/html

    <html>   
    <head>
    <title>jobbofhe</title>
    </head>
    <html>
    <p>hi, python</p>
    </html>
    '''
    method = request.decode('utf-8').split(' ')[0]
    if method == 'GET':
        conn.sendall(response)

    conn.close()
134 artículos originales publicados · Me gustaron 119 · Visite 310,000+

Supongo que te gusta

Origin blog.csdn.net/jobbofhe/article/details/90408137
Recomendado
Clasificación