使用 Twisted 进行基本 HTTP 解析

发布于 2024-09-06 04:01:10 字数 770 浏览 3 评论 0原文

我是 Python 和 Twisted 游戏的新手,所以请原谅我可能会问这个问题的无知。作为第一个程序,我尝试使用twisted.web.sever 编写一个基本的HTTP 服务器,它将简单地打印以屏幕HTTP 请求,然后打印以屏幕HTTP 响应。我正在尝试打印整个消息。这是我到目前为止所得到的:

from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
import time

class TestPage(Resource):
    isLeaf = True
    def render_GET(self, request):
        response = "Success"
        print "You're request was %s" % request
        print "The sever's response was %s" % response
        return response

resource = TestPage()
factory = Site(resource)
reactor.listenTCP(8000, factory)
reactor.run()

到目前为止,我已经成功打印了请求。我想知道的是在哪里可以访问原始响应数据,而不仅仅是文本消息。另外,如果我想开始解析信息的请求/响应,最好的方法是什么?

编辑:我也是 stackoverflow 的新手,如何让此代码正确显示?

I am a newcomer to the Python and Twisted game so excuse the ignorance I will likely be asking this question with. As a sort of first program, I am trying to write a basic HTTP server using twisted.web.sever which would simply print to screen the HTTP request, and then print to screen the HTTP response. I am trying to print the entire message. Here is what I have so far:

from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
import time

class TestPage(Resource):
    isLeaf = True
    def render_GET(self, request):
        response = "Success"
        print "You're request was %s" % request
        print "The sever's response was %s" % response
        return response

resource = TestPage()
factory = Site(resource)
reactor.listenTCP(8000, factory)
reactor.run()

So far, I am having success printing the request. What I want to know is where I can access the raw response data, not just the textual message. Also, if I wanted to start parsing the request/response for information, what would be the best way to go about doing that?

Edit: I'm also new to stackoverflow, how do I get this code to display properly?

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

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

发布评论

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

评论(1

思念满溢 2024-09-13 04:01:10

查看 请求IRequest API 文档以了解该 request 参数为您提供。您应该能够在那里找到请求中的几乎所有内容。

不过,我不确定原始响应数据是什么意思。响应由您来生成。

Take a look at the Request and IRequest API docs to get an idea of what that request parameter offers you. You should be able to find just about everything in the request there.

I'm not sure what you mean by raw response data though. The response is up to you to generate.

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