使用 Twisted 进行基本 HTTP 解析
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 请求 和 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.