After spending more time then necessary trying to figure out how to use pycurl to login into a website, save the cookie, then use the cookie. I just wanted a simple example script that I can work off, but I couldn’t find any. I finally figured out pycurl, and here is really simple example of how to use pycurl.
Using pycurl to login into a website (curl-login.py)
#! /usr/bin/env python # -*- coding: utf-8 -*- # vi:ts=4:et # $Id: curl-login.py,v 1.5 2005/02/11 11:09:11 mfx Exp$ import pycurl curlobj=pycurl.Curl() curlobj.setopt(pycurl.URL, 'http://websiteforum.org/ucp.php?mode=login') curlobj.setopt(pycurl.POSTFIELDS, 'username=andey&password=mypassword&login=Login'); curlobj.setopt(pycurl.COOKIEFILE, "/home/andey/Downloads/cookies.txt") curlobj.setopt(pycurl.COOKIEJAR, "/home/andey/Downloads/cookies.txt") curlobj.perform() curlobj.close()
Now view a page that requires authentication with a cookie (curl-read.py)
#! /usr/bin/env python # -*- coding: utf-8 -*- # vi:ts=4:et # $Id: curl-read.py,v 1.5 2005/02/11 11:09:11 mfx Exp$ import sys, os, re, pycurl, StringIO strio=StringIO.StringIO() curlobj=pycurl.Curl() curlobj.setopt(pycurl.URL, 'http://websiteforum.org/viewtopic.php?f=25&t=2375060') curlobj.setopt(pycurl.COOKIEFILE, "/home/andey/Downloads/cookies.txt") curlobj.setopt(pycurl.WRITEFUNCTION, strio.write) curlobj.perform() curlobj.close() print strio.getvalue()