从应用程序捕获完整请求

2024-04-19 16:48:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个Python应用程序,它可以查询多个web服务器,每台服务器每分钟查询一次。应用程序可以正常运行几分钟,然后所有服务器都开始响应,并出现以下错误:

The following cURL error occurred: The requested URL returned error: 400 Bad Request

当这种情况发生时,我看到我的输出带宽峰值和两个CPU核心都工作在80%以上(这台机器上名义上非常轻的负载)。然而,内存的使用并没有受到明显的影响。在

是否有任何方法可以捕获从特定应用程序发送的所有请求的整个请求?或者,在Python中,我是否可以捕获完整的请求,然后将其输出回用于调试目的?以下是查询服务器的代码部分:

output = cStringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0')
c.setopt(c.WRITEFUNCTION, output.write)
c.setopt(c.CONNECTTIMEOUT, 10) 
c.setopt(c.TIMEOUT, 15) 
c.setopt(c.FAILONERROR, True)
c.setopt(c.NOSIGNAL, 1)

try:
    c.perform()
    toReturn = output.getvalue()
    output.close()
    return toReturn

except pycurl.error, error:
    errno, errstr = error
    print 'The following cURL error occurred: ', errstr

这台机器是一个双核2.0GHz的Intel机器,内存为6GiB,运行Kubuntu12.10和Python2.7。所有东西都是用VIM编码的,所以没有虚假的IDE代码在运行。在


Tags: the内存代码服务器机器应用程序urloutput
1条回答
网友
1楼 · 发布于 2024-04-19 16:48:31

Is there any way to capture the entire request for all requests sent from a particular application?

捕获和监视所有传入/传出流量(包括从应用程序发送的请求)的最简单方法是使用wireshark

Alternatively, in Python can I capture the full request and then output it back for debugging purposes?

您可以添加以下调试选项,以便curl打印有关请求的一些有用信息:

c.setopt(pycurl.VERBOSE, 1)

相关问题 更多 >