1

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.

3
  • 1
    "I cannot get this login script to work". You should perhaps define what it is doing and why you don't like that. Does it have Syntax Errors? Does it return a 401 error from the web server? What are you seeing as the result? Commented Dec 1, 2010 at 17:53
  • My mistake, when I view the interaction in Wireshark the server simply returns the same login page that I posted to (i.e. I am not redirected). Other than that no errors, nothing. The only difference I can find is when I submit the form in Chrome the 302 response packet contains cookie information in the http header and it does not when I use the script... hmmm maybe I should reword my question. Commented Dec 1, 2010 at 18:32
  • "maybe I should reword my question". Absolutely. Please update the question to be very clear on what problem you're having. Commented Dec 1, 2010 at 21:36

1 Answer 1

2

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() 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.