Python Login Script

I know similar questions have been asked, I have read them. I have also read most other related articles I could find. I have tried httplib2, header modifications, and anything else I could find or think of, however I cannot get this login script to work.

import cookielib
import urllib
import urllib2

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
resp = opener.open('http://www.biocidecity.com')

theurl = 'http://www.biocidecity.com/index.php'
body={'username':'someusername','password':'somepassword', 'login' : '1'}
txdata = urllib.urlencode(body) txheaders =  {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}


try:
    req = urllib2.Request(theurl, txdata, txheaders)
    handle = opener.open(req) 
    HTMLSource = handle.read()
    f = file('test.html', 'w')
    f.write(HTMLSource)
    f.close()

except IOError, e:
    print 'We failed to open "%s".' % theurl
    if hasattr(e, 'code'):
        print 'We failed with error code - %s.' % e.code
    elif hasattr(e, 'reason'):
        print "The error object has the following 'reason' attribute :", e.reason
        print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
        sys.exit()

else:
    print 'Here are the headers of the page :'
    print handle.info() 

First this is not my script however I have modified it some. Second I think it has something to do with the way the cookies are handled but I can't figure it out. I also replaced the username password combination.

Answers


I suppose it's again term of service but you should try

import cookielib
import urllib
import urllib2

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
resp = opener.open('http://www.biocidecity.com')

theurl = 'http://www.biocidecity.com/index.php'
body={'username':'someusername','password':'somepassword', 'login' : '1'}
txdata = urllib.urlencode(body) txheaders =  {'Referer': 'http://www.biocidecity.com/index.php'}


try:
    req = urllib2.Request(theurl, txdata, txheaders)
    handle = opener.open(req) 
    HTMLSource = handle.read()
    f = file('test.html', 'w')
    f.write(HTMLSource)
    f.close()

except IOError, e:
    print 'We failed to open "%s".' % theurl
    if hasattr(e, 'code'):
        print 'We failed with error code - %s.' % e.code
    elif hasattr(e, 'reason'):
        print "The error object has the following 'reason' attribute :", e.reason
        print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
        sys.exit()

else:
    print 'Here are the headers of the page :'
    print handle.info() 

Need Your Help

What is the best way to open a file in a browser

php javascript html5 download

I know of a few ways (i know there is probably more) to open a file in a browser after the user clicks on a link,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.