python 循环接收http请求数据

最近做一个项目需要测试http请求,大量密集的http请求测试,网上只找到了postman这种client工具,没有发现http server工具,于是想手写一个。听闻python快捷,网上找了一个,但是只能接收一次数据,于是改动了一下,测试了一下效果还不错,这里记录一下。

# coding:utf-8
#环境:python2.7
import socket

from multiprocessing import Process


if __name__ == "__main__":
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(("", 8000))
    server_socket.listen(128)
    client_socket, client_address = server_socket.accept()
    while True:
        request_data = client_socket.recv(10240)
        print("request data:", request_data)
        # 构造响应数据
        response_start_line = "HTTP/1.1 200 OK\r\n"
        response_headers = "Server: received\r\n"
        response_body = "

Python HTTP Test

" response = response_start_line + response_headers + "\r\n" + response_body # 向客户端返回响应数据 client_socket.send(bytes(response))

 

你可能感兴趣的:(python)