Pycurl 在终端中保持打印

发布于 2024-12-08 12:42:13 字数 344 浏览 0 评论 0原文

我是一个初学者,使用 Python 和 Pycurl 进行网页压力测试。然而,pycurl 不断在终端中打印返回的 html,这使得压力测试花费的时间比应有的时间更长。下面发布了我正在使用的一个这样的 pycurl 代码。有没有办法只运行 pycurl 而不必在任何地方打印或写入结果?任何帮助将不胜感激。

p = pycurl.Curl()
p.setopt(pycurl.POST, 0)
p.setopt(pycurl.COOKIE, sessioncookie)
p.setopt(pycurl.URL, 'http://example.com/logoff.php')
p.perform()
p.close()

I am a beginner using Python and Pycurl for webpage stressing testing purposes. However, pycurl keeps printing out returned html in the terminal which makes the stress testing take even more time than it should. One such pycurl code I am using is posted below. Is there a way to just run pycurl without having to print or write the result anywhere? Any assistance would be appreiciated.

p = pycurl.Curl()
p.setopt(pycurl.POST, 0)
p.setopt(pycurl.COOKIE, sessioncookie)
p.setopt(pycurl.URL, 'http://example.com/logoff.php')
p.perform()
p.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我三岁 2024-12-15 12:42:13

Pycurl 文档很糟糕,但我认为您想将 WRITEFUNCTION 设置为一个不执行任何操作的函数,例如

p.setopt(pycurl.WRITEFUNCTION, lambda x: None)

另外,我希望郑重声明,我认为“SET 可以做所有事情”API 随 VMS 一起推出。嘎啊。

The Pycurl documentation is terrible, but I think you want to set WRITEFUNCTION to a function that does nothing, e.g.

p.setopt(pycurl.WRITEFUNCTION, lambda x: None)

Also, I wish to state for the record that I thought "SET does everything" APIs went out with VMS. Gaaah.

旧城烟雨 2024-12-15 12:42:13

可以试试这个吗?

devnull = open('/dev/null', 'w')
p.setopt(pycurl.WRITEFUNCTION, devnull.write)

或者只是一个什么都不做的函数。

Could try this?

devnull = open('/dev/null', 'w')
p.setopt(pycurl.WRITEFUNCTION, devnull.write)

or just a function that does nothing.

若有似无的小暗淡 2024-12-15 12:42:13

我对这里列出的两种方法都没有任何运气。两者都会导致以下错误:

pycurl.error: (23, 'Failed writing body (0 != 108)')

根据文档,lambda x: Nonedevnull.write 应该是不错的选择:

WRITEFUNCTION 回调可能返回写入的字节数。如果该数字不等于字节字符串的大小,则表示出现错误,libcurl 将中止请求。 返回 None 是另一种方式,表明回调已消耗了传递给它的所有字符串,因此成功。

http://pycurl.sourceforge.net/doc/callbacks.html#WRITEFUNCTION

然而,在我的项目中,我必须执行以下操作来解决此问题:

c.setopt(pycurl.WRITEFUNCTION, lambda bytes: len(bytes))

换句话说,返回我查看时写入的字节数不是可选的。 devnull.write 实际上确实返回了写入的字节数,但我没有研究这一点。字节与字符串可能存在一些问题。

请注意,我使用的是 Python 3。我猜这不适用于 Python 2。

I haven't had any luck with both approaches listed here. Both lead to the following error:

pycurl.error: (23, 'Failed writing body (0 != 108)')

According to documentation both lambda x: None and devnull.write should be good options:

The WRITEFUNCTION callback may return the number of bytes written. If this number is not equal to the size of the byte string, this signifies an error and libcurl will abort the request. Returning None is an alternate way of indicating that the callback has consumed all of the string passed to it and, hence, succeeded.

http://pycurl.sourceforge.net/doc/callbacks.html#WRITEFUNCTION

However in my project I had to do the following to fix this problem:

c.setopt(pycurl.WRITEFUNCTION, lambda bytes: len(bytes))

In other words it was not optional to return the number of bytes written when I looked. devnull.write actually does return the number of bytes written, I didn't look into that though. Possibly there's some issue with bytes vs strings.

Note that I'm using Python 3. I'm guessing this does not apply to Python 2.

梦断已成空 2024-12-15 12:42:13

要隐藏输出,请将 VERBOSE 更改为 0:

p.setopt(pycurl.VERBOSE, 0)

To hide the output, change the VERBOSE to 0:

p.setopt(pycurl.VERBOSE, 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文