小编Ove*_*ang的帖子

无法使用Twisted获取HTTP POST请求正文

我试图通过使用tpbasic.LineReceiver获取HTTP POST请求正文但失败了.我的代码如下:

from twisted.internet import reactor, protocol
from twisted.protocols import basic

class PrintPostBody(basic.LineReceiver):
    def __init__(self):
        self.line_no = 0

    def lineReceived(self, line):
        print '{0}: {1}'.format(str(self.line_no).rjust(3), repr(line))
        self.line_no += 1

    def connectionLost(self, reason):
        print "conn lost"

class PPBFactory(protocol.ServerFactory):
    protocol = PrintPostBody

def main():
    f = PPBFactory()
    reactor.listenTCP(80, f)
    reactor.run()


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

但是当我在端口80对该机器发出HTTP POST请求时,只打印出HTTP请求头.样本输出:

  0: 'POST / HTTP/1.0'
  1: 'Host: ###.##.##.##'
  2: 'Referer: http://#.#####.###/?ssid=0&from=0&bd_page_type=1&uid=wiaui_1292470548_2644&pu=sz%40176_229,sz%40176_208'
  3: 'Content-Length: 116'
  4: 'Origin: http://#.#####.###'
  5: 'Content-Type: application/x-www-form-urlencoded'
  6: 'Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  7: 'User-Agent: Mozilla/5.0 (X11; …
Run Code Online (Sandbox Code Playgroud)

python http twisted

2
推荐指数
1
解决办法
3126
查看次数

标签 统计

http ×1

python ×1

twisted ×1