Would love some feedback on my first script

134 views
Skip to first unread message

Daniel Sutcliffe

unread,
Apr 5, 2011, 6:44:46 PM4/5/11
to multi-mechanize
Sorry for spamming the list on my first day, but this is starting to
be fun and I feel like sharing.

The below script is where I'm at - seems to work well for my app
although doesn't really exercise it yet. Learning Python at the same
time as doing this is ... interesting, so I'd appreciate anything
constructive anyone has to say on any aspect of it.

#!/usr/bin/env python
#
# First attempt at a multi-mechanize test script
#
import mechanize
import time
import sys

BASEURL = 'http://example.com/'

class Transaction(object):
def __init__(self):
self.custom_timers = {}
self.br = mechanize.Browser()
self.br.set_handle_robots(False)

def run(self):
thid = "{0:03d}:{1:03d}".format(self.process_num,
self.thread_num)

resp = self.br.open(BASEURL)
resp.read()
assert(resp.code == 200), 'Bad HTTP Response'
print thid, self.br.title(), self.br.geturl()
try:
self.br.select_form(name='auth_form')
except mechanize.FormNotFoundError:
try:
print "Could not find login form, attempting logout"
self.br.select_form(name='logout_form')
resp = self.br.submit()
resp.read()
self.br.select_form(name='auth_form')
except:
print "Could not find logout form either, abandon
everything"
raise
except:
sys.exit('Unexpected Error locating login form');

time.sleep(1)

self.br['authenticate[userid]'] =
'loadvu{0:03d}'.format(self.thread_num)
self.br['authenticate[password]'] = 'a'
start_timer = time.time()
resp = self.br.submit()
resp.read()
assert(resp.code == 200), 'Bad HTTP Response'
self.custom_timers['login'] = time.time() - start_timer
print thid, self.br.title(), self.br.geturl()

time.sleep(1)

try:
self.br.select_form(name='logout_form')
except:
sys.exit('Unexpected Error locating logout form');
resp = self.br.submit()
resp.read()

if __name__ == '__main__':
trans = Transaction()
trans.run()
print trans.custom_timers

Cheers
/dan

Corey Goldberg

unread,
Apr 6, 2011, 11:55:21 AM4/6/11
to multi-m...@googlegroups.com
hey Daniel,

great to see you getting using Multi-Mech!

it looks like you are on the right path. I'm a little swamped, but
will try to review your script and answer other questions when I get a
moment.

-Corey

Reply all
Reply to author
Forward
Message has been deleted
0 new messages