python - 如何在 python + twisted 中检测 HTTP 请求?

标签 python http twisted packet

我正在使用 python 中的 twisted 10 学习网络编程。在下面的代码中,有什么方法可以在收到数据时检测 HTTP 请求?还从中检索域名、子域、端口值?如果不是 http 数据就丢弃它?

from twisted.internet import stdio, reactor, protocol

from twisted.protocols import basic

import re



class DataForwardingProtocol(protocol.Protocol):

    def _ _init_ _(self):

        self.output = None

        self.normalizeNewlines = False



    def dataReceived(self, data):

        if self.normalizeNewlines:

            data = re.sub(r"(\r\n|\n)", "\r\n", data)

        if self.output:

            self.output.write(data)



class StdioProxyProtocol(DataForwardingProtocol):

    def connectionMade(self):

        inputForwarder = DataForwardingProtocol( )

        inputForwarder.output = self.transport

        inputForwarder.normalizeNewlines = True

        stdioWrapper = stdio.StandardIO(inputForwarder)

        self.output = stdioWrapper

        print "Connected to server.  Press ctrl-C to close connection."



class StdioProxyFactory(protocol.ClientFactory):

    protocol = StdioProxyProtocol



    def clientConnectionLost(self, transport, reason):

        reactor.stop( )



    def clientConnectionFailed(self, transport, reason):

        print reason.getErrorMessage( )

        reactor.stop( )



if __name__ == '_ _main_ _':

    import sys

    if not len(sys.argv) == 3:

        print "Usage: %s host port" % _ _file_ _

        sys.exit(1)



    reactor.connectTCP(sys.argv[1], int(sys.argv[2]), StdioProxyFactory( ))

    reactor.run( )

最佳答案

protocol.dataReceived ,你要覆盖的,级别太低,无法在没有你没有做的智能缓冲的情况下达到目的——根据我刚刚引用的文档,

Called whenever data is received.

Use this method to translate to a higher-level message. Usually, some callback will be made upon the receipt of each complete protocol message.

Parameters

data

a string of indeterminate length. Please keep in mind that you will probably need to buffer some data, as partial (or multiple) protocol messages may be received! I recommend that unit tests for protocols call through to this method with differing chunk sizes, down to one byte at a time.

您似乎完全忽略了文档的这一关键部分。

您可以改为使用 LineReceiver.lineReceived (当然,继承自 protocols.basic.LineReceiver)以利用 HTTP 请求以“行”形式出现的事实——您仍然需要连接作为多个发送的 header 行,因为 this tutorial说:

Header lines beginning with space or tab are actually part of the previous header line, folded into multiple lines for easy reading.

一旦您有了格式良好/经过解析的响应(考虑研究 twisted.web's sources 以便找到一种可以完成的方法),

retrieve Domain name, Sub Domain, Port values from this?

现在 Host header (参见 the RFC 第 14.23 节)包含此信息。

关于python - 如何在 python + twisted 中检测 HTTP 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3430689/

相关文章:

python - 使用代理找出哪个响应属于哪个请求

http - 将 Go 网络服务器部署到 Google 计算引擎

python - Twisted 中是否有用于 .tac 文件的 Emacs 模式?

python - 为什么 Twisted Transports 没有接收数据的方法?

python - 如果列的任何单元格中存在True,该如何测试?

python - 有什么方法可以将 python 集转换为在 SQL 的 "IN"语句中使用?

python - 如何从其他python程序访问django数据库?

python - 在 Python 3 中使用 RSA,需要对消息进行签名(不对消息进行哈希处理)

html - 如果提交的表单包含验证错误,我是否应该返回 400 错误?

python - Python 的 Twisted 中 LoopingCall 和 callInThread 的区别