收听HTTP响应 [英] Listening for HTTP responses

查看:126
本文介绍了收听HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个将在套接字上侦听HTTP响应的python应用程序。我正在使用 http-parser 。这是我的代码:

I am trying to write a python application that will listen for HTTP responses on a socket. I am using http-parser for this. Here is my code:

#!/usr/bin/env python
import socket

from http_parser.http import HttpStream
from http_parser.reader import SocketReader

from http_parser.util import b

def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((socket.gethostname(), 7000))
    s.listen(5)
    try:
        while True:
            p = HttpStream(SocketReader(s))
    finally:
        s.close()

if __name__ == "__main__":
    main()

我有两个问题:


  1. 这是最好的方法吗?请注意,我不想发送请求然后侦听响应。我希望有一个重定向机制,将所有响应重定向到运行此脚本的框。

  2. 我该如何测试?有没有可以模拟HTTP响应的工具?

编辑
我想要的是什么这是这样的:我有三个盒子,一旦运行Apache,一个运行这个脚本,一个是客户端。当客户端连接到Apache并发回响应时,我将响应转移到此框。所以在这个脚本中,我正在尝试监听HTTP响应。

EDIT What I am trying to do is this: I have three boxes, once runs Apache, one runs this script and one is the client. When the client connects to Apache and it sends back a response, I am diverting the response to this box. So in this script, I am trying to listen for HTTP responses.

拓扑
这是我的拓扑:
服务器< ---->切换一个< - --->切换2< ----->第一个和第二个
最初,第一个框连接到服务器并发送请求。当第二个交换机收到来自服务器的响应时,它会将它分配到第一个框和第二个框。

Topology Here is my topology: Server <----> Switch one <-----> Switch two <-----> Box one and two Initially, box one connects to the server and sends a request. When the second switch receives responses from the server, it forks it to both box one and box two.

推荐答案

我最终使用原始套接字并直接从中读取数据包。

I ended up using a raw socket and directly reading packets from it.

这篇关于收听HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆