python http 处理程序

发布于 2024-08-15 02:31:24 字数 496 浏览 7 评论 0原文

我想要类似 BaseHTTPRequestHandler 的东西,除了我不希望它绑定到任何套接字;我想自己处理传入和传出的原始 HTTP 数据。有没有什么好的方法可以让我在 Python 中做到这一点?

为了澄清,我想要一个从Python(不是套接字)接收原始TCP数据的类,处理它并返回TCP数据作为响应(再次返回到python)。因此,此类将处理 TCP 握手,并且将具有覆盖我在 HTTP GET 和 POST 上发送的内容的方法,例如 do_GETdo_POST。因此,我想要类似已经存在的服务器基础设施的东西,但我想在 python 中传递所有原始 TCP 数据包,而不是通过操作系统套接字。

I want something like BaseHTTPRequestHandler, except that I don't want it to bind to any sockets; I want to handle the raw HTTP data to and from it myself. Is there a good way that I can do this in Python?

To Clarify, I want a class that receives raw TCP data from Python (NOT a socket), processes it and returns TCP data as a response (to python again). So this class will handle TCP handshaking, and will have methods that override what I send on HTTP GET and POST, like do_GET and do_POST. So, I want something like the Server infrastructure that already exists, except I want to pass all raw TCP packets in python and not through operating system sockets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-08-22 02:31:24

BaseHTTPRequestHandler 派生自 StreamRequestHandler,它基本上从文件 self.rfile 读取并写入 self.wfile,因此您可以从BaseHTTPRequestHandler派生一个类并提供您自己的rfile和wfile,例如

import StringIO
from  BaseHTTPServer import BaseHTTPRequestHandler

class MyHandler(BaseHTTPRequestHandler):

    def __init__(self, inText, outFile):
        self.rfile = StringIO.StringIO(inText)
        self.wfile = outFile
        BaseHTTPRequestHandler.__init__(self, "", "", "")

    def setup(self):
        pass

    def handle(self):
        BaseHTTPRequestHandler.handle(self)

    def finish(self):
        BaseHTTPRequestHandler.finish(self)

    def address_string(self):
        return "dummy_server"

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("<html><head><title>WoW</title></head>")
        self.wfile.write("<body><p>This is a Total Wowness</p>")
        self.wfile.write("</body></html>")

outFile = StringIO.StringIO()

handler = MyHandler("GET /wow HTTP/1.1", outFile)
print ''.join(outFile.buflist)

输出:

dummy_server - - [15/Dec/2009 19:22:24] "GET /wow HTTP/1.1" 200 -
HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.5.1
Date: Tue, 15 Dec 2009 13:52:24 GMT
Content-type: text/html

<html><head><title>WoW</title></head><body><p>This is a Total Wowness</p></body></html>

BaseHTTPRequestHandler derives from StreamRequestHandler, which basically reads from file self.rfile and writes to self.wfile, so you can derive a class from BaseHTTPRequestHandler and supply your own rfile and wfile e.g.

import StringIO
from  BaseHTTPServer import BaseHTTPRequestHandler

class MyHandler(BaseHTTPRequestHandler):

    def __init__(self, inText, outFile):
        self.rfile = StringIO.StringIO(inText)
        self.wfile = outFile
        BaseHTTPRequestHandler.__init__(self, "", "", "")

    def setup(self):
        pass

    def handle(self):
        BaseHTTPRequestHandler.handle(self)

    def finish(self):
        BaseHTTPRequestHandler.finish(self)

    def address_string(self):
        return "dummy_server"

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("<html><head><title>WoW</title></head>")
        self.wfile.write("<body><p>This is a Total Wowness</p>")
        self.wfile.write("</body></html>")

outFile = StringIO.StringIO()

handler = MyHandler("GET /wow HTTP/1.1", outFile)
print ''.join(outFile.buflist)

Output:

dummy_server - - [15/Dec/2009 19:22:24] "GET /wow HTTP/1.1" 200 -
HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.5.1
Date: Tue, 15 Dec 2009 13:52:24 GMT
Content-type: text/html

<html><head><title>WoW</title></head><body><p>This is a Total Wowness</p></body></html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文