0

Hello all, I have been trying for a few days now and done quite a bit of reasearch on the internet and this website to find out how I can authenicate to a website. It could very well be that this particualar web server is picky I'm not to sure. It would be great if I could get some help with this, I would also recommend the site to anybody with some free time on their hands and that doesn't wanna sit around thinking of ideas for programs all day. They have a fun list of entertaining challenges. Here is the code that I have came up with so far but no success in logging in.

import urllib, urllib2, cookielib
username = 'username'
password = 'password'
host = 'www.hackthissite.org'
user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0)'
referer = 'http://www.hackthissite.org/user/login'
content_type = 'application/x-www-form-urlencoded'
accept_encoding = 'gzip,deflate'

values = {'Host': 'www.hackthissite.org',
            'User-Agent':'Mozilla/4.0 (compatible; MSIE 8.0)',
            'Referer':'http://www.hackthissite.org/user/login',
            'Content-Type':'application/x-www-form-urlencoded',
            'Accept-Encoding':'gzip,deflate',
        }

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
data = urllib.urlencode(values)
url = referer
req = urllib2.Request(url, data)
page = urllib2.urlopen(req)
req = urllib2.Request("http://www.hackthissite.org/missions/prog/1/", data)
page2 = urllib2.urlopen(req)

print page.info()
print page.geturl()
print page2.info()
print page2.geturl()

print page2.read()

Thanks a bunch ahead of time. :D

Edited by willygstyle: messed up code tags

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by willygstyle
0

I guess I jumped the gun a little bit, I figured out what I was doing wrong. Kinda silly actually, but I sure learned alot from my lack of paying attention to details. Anyway there was a guy who solved it on their site using httplib2, but I'm using py2.6 and couldn't download it. So if anyone is interested I'll post my solution with just urllib and urllib2.

import urllib, urllib2, cookielib

host = 'www.hackthissite.org'
user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0)'
referer = 'http://www.hackthissite.org/user/login'
content_type = 'application/x-www-form-urlencoded'
accept_encoding = 'gzip,deflate'

body = {'username': 'username', 'password': 'password'}


values = {'Host': 'www.hackthissite.org',
          'User-Agent':'Mozilla/4.0 (compatible; MSIE 8.0)',
          'Referer':'http://www.hackthissite.org/user/login',
          'Content-Type':'application/x-www-form-urlencoded',
          'Accept-Encoding':'gzip,deflate',
          }

#build cookie handler
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

#encode request information
data = urllib.urlencode(body)

#send login information
req = urllib2.Request(referer, data, values)
page = urllib2.urlopen(req)


#start opening pages
url = "http://www.hackthissite.org/missions/prog/1/"


page2 = urllib2.urlopen(url)

print page.info()
print "------------------"
print page2.read()
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.