Logging in and using cookies in pycurl

I need to upload a file that is on a password protected page. To go to the page manually, I first need to go through authentication through a regular login page. I want to use curl to get this page in a script.
My script is the first entry. It seems to succeed - it returns 200 from PUT to / login. However, fetching the desired page fails with a 500 error.

I use a cookie jar:

C.setopt(pycurl.COOKIEJAR, 'cookie.txt') 

In detailed mode, I can see that cookies will be exchanged when I receive the file I need. Now my question is: is there still a use of COOKIEJAR?

+7
python curl pycurl
source share
3 answers

I believe Curl will save cookies, but you should use them explicitly. However, I used the command line interface for this. Documentation scanning, I think you can try:

 C.setopt(pycurl.COOKIEFILE, 'cookie.txt') 

(before the second request)

+11
source share

You must first save the cookie and then read it:

 C.setopt(pycurl.COOKIEJAR, 'cookie.txt') C.setopt(pycurl.COOKIEFILE, 'cookie.txt') 

Here is what curl --help returned:

 -b, --cookie STRING/FILE String or file to read cookies from (H) -c, --cookie-jar FILE Write cookies to this file after operation (H) 

See this sample:

 def connect(self): ''' Connect to NGNMS server ''' host_url = self.ngnms_host + '/login' c = pycurl.Curl() c.setopt(c.URL, host_url) c.setopt(pycurl.TIMEOUT, 10) c.setopt(pycurl.FOLLOWLOCATION, 1) c.setopt(pycurl.POSTFIELDS, 'j_username={ngnms_user}&j_password={ngnms_password}'.format(**self.ngnms_login)) c.setopt(pycurl.COOKIEJAR, 'data/ngnms.cookie') # c.setopt(c.VERBOSE, True) c.setopt(pycurl.SSL_VERIFYPEER, 0); session = c return session 
+7
source share

wds is correct.

for your further edification, the available options are based on the values โ€‹โ€‹of http://curl.haxx.se/libcurl/c/curl_easy_setopt.html (see the section on cookie shortcuts).

500 is an internal server error ... it's hard to be sure if this can be blamed on your script without knowing more about what is happening here. you can not transfer other data that the page expects (not related to cookies) for everyone we know (and they did not implement graceful error handling!)

Db

+2
source share

All Articles