Amir
Amir

Reputation: 496

Disconnecting the connection in pycurl

I'm writing a python code to download files using pycurl. Is there any possibility to inform my program to disconnect the connection? I mean that while the code is running, I want to disconnect the connection whenever I want. (e.g. by clicking on a button and invoking a function). It can be used to pause the download.

Upvotes: 1

Views: 367

Answers (2)

woozyking
woozyking

Reputation: 5220

Ah yes. To answer your question's easier part, the solution's to perform a "malicious" return statement in your pycurl.WRITE_FUNCTION.

An example here in my project demonstrates how to actually disconnect a theoretically never-ending HTTP GET request (streaming)

def _on_data_receive(self, data):
    self.buffer += data

    if data.endswith('\r\n'):  # or any valid chunk delimiter
        # Do something about self.buffer
        print self.buffer

    if True:  # some valid condition to disconnect
        return -1  # the way to stop the stream, would raise pycurl.error

Upvotes: 1

user1630938
user1630938

Reputation:

  • Yes, you could use callbacks see those examples from PyCurl Doc
  • A lot of C examples from libcurl are very helpful LibCurl
    you easily can use those options in python

Upvotes: 1

Related Questions