10

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()
jyim89
  • 245
  • 3
  • 12

4 Answers4

27

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.

zwol
  • 135,547
  • 38
  • 252
  • 361
3

Could try this?

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

or just a function that does nothing.

Amber
  • 507,862
  • 82
  • 626
  • 550
1

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.

href_
  • 1,509
  • 16
  • 15
0

To hide the output, change the VERBOSE to 0:

p.setopt(pycurl.VERBOSE, 0)