A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

Question about python

807 views · started by takoyaki ·
#1
Question about python
I am new to dealing with this so I am sorry if these are dumb questions.
How do you know if python was successful when logging into a website.
for now I am using stock code in their guides, so for example.

import urllib2
import sys
import re
import base64
from urlparse import urlparse

theurl = 'http://www.someserver.com/somepath/somepage.html'
# if you want to run this example you'll need to supply
# a protected page with your username and password

username = 'johnny'
password = 'XXXXXX'

req = urllib2.Request(theurl)
try:
handle = urllib2.urlopen(req)
except IOError, e:

pass
else:

if not hasattr(e, 'code') or e.code != 401:


authline = e.headers['www-authenticate']



authobj = re.compile(
r'''(?:\s*www-authenticate\s*:)?\s*(\w*)\s+realm=['"]([^'"]+)['"]''',
re.IGNORECASE)


if not matchobj:

print 'The authentication header is badly formed.'
print authline
sys.exit(1)

scheme = matchobj.group(1)
realm = matchobj.group(2)

if scheme.lower() != 'basic':
print 'This example only works with BASIC authentication.'
sys.exit(1)

base64string = base64.encodestring(
'%s:%s' % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
try:
handle = urllib2.urlopen(req)
except IOError, e:

print "It looks like the username or password is wrong."
sys.exit(1)
thepage = handle.read()

say i run this with my gmail information, and nothing happens, does that mean it succeeded. Or does the page need a pop up authentication?

Thank you for any input.
#2
Seems like you're trying to login with the wrong protocol. This script is used to those pages that pop-up a text box asking you for username and password, and if you close the box it re-directs you to another page. I don't have a page for that example, however I have my own HTTP class I can post. It's pretty simple and easy to learn from.
#3
Riddle wrote:
Seems like you're trying to login with the wrong protocol. This script is used to those pages that pop-up a text box asking you for username and password, and if you close the box it re-directs you to another page. I don't have a page for that example, however I have my own HTTP class I can post. It's pretty simple and easy to learn from.


yeah I assumed what I was looking at was for pop up login but I havent seen one of those in forever so was wondering.
I would love to see the http class.
Python seems very interesting, the little I have looked at seems fairly easy to use, and the guides you can find online and in its own guide section are fairly useful.

thanks for the response man