[Tutor] Python mechanize: Auto follow meta refresh?

Troels Emtekær Linnet tlinnet at gmail.com
Tue Aug 9 15:34:28 CEST 2011


Hi.

I try to to make a python interaction with the form on:
http://propka.ki.ku.dk/
I just try to set: version='v3.1' and PDBID='4AKE'

After the click and sending the form it stops.
It should automatically refresh.

Here is my code:

import mechanize, sys, time, os

url = "http://propka.ki.ku.dk/"
version = "v3.1"
PDBID = "4AKE"
verbose = 'yes'
server_wait = 1.0

br = mechanize.Browser()
br.addheaders = [('User-agent', 'pythonMechanizeClient')]
### The things was added according to:
http://stockrt.github.com/p/emulating-a-browser-in-python-with-mechanize/
### Dont know what they do.
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Turn on debugging messages
br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)

openpage = br.open(url)
readpage = openpage.read()
if verbose == 'yes': print br.title(); print br.geturl()
#print readpage

# To get available forms
forms = [f.name for f in br.forms()]
if verbose == 'yes': print forms

## Select first form
br.select_form(name=forms[0])
## Print the parameters of the 'version' RadioControl button and current
value
if verbose == 'yes': print br.find_control(name='version'); print
br.find_control(name='version').value
## Change the parameters of the 'version' radio button and then reprint the
new value
br.form['version'] = [version]
if verbose == 'yes': print br.find_control(name='version').value#
## Print the parameters of the 'PDBID' TextControl button and current value
if verbose == 'yes': print br.find_control(name='PDBID'); print
br.find_control(name='PDBID').value
## Change the parameters of the 'PDBID' TextControl and then reprint the new
value
br.form["PDBID"] = PDBID
if verbose == 'yes': print br.find_control(name='PDBID').value
## Now reprint the current selected form, so we see that we have the right
values.
if verbose == 'yes': print br.form

## Two ways of submitting is tested
#br.submit()
req = br.click(type="submit", nr=0)

### Have to pass by a mechanize exception. Thats the reason for the why True
### The error was due to:
#br.open(req)
##### mechanize._response.httperror_seek_wrapper: HTTP Error refresh: The
HTTP server returned a redirect error that would lead to an infinite loop.
##### The last 30x error message was:
##### OK
while True:
    try:
        br.open(req)
        break
    ### If there is raised an exception, we make a break.
    except:
        print("Waiting %s seconds for server"%server_wait)
        time.sleep(server_wait)
        break
print br.geturl()
print br.title()
print br.response().read()
## Looking at some results in link format
for l in br.links():
    print l
#for l in br.links(url_regex='stockrt'):
#    print l
*
It returns:*

<HTML>
<HEAD>
<TITLE>PROPKA Progress</TITLE>
<link rel="stylesheet" href="http://propka.ki.ku.dk/baker.css"
type="text/css">
<meta http-equiv="Refresh" content="1; url=/pka/temp-4AKE.html">
<META HTTP-EQUIV="expires" CONTENT="Wed, 09 Aug 2000 08:21:57 GMT"></HEAD>
<BODY>
<H2>PROPKA Progress</H2><P>
The PROPKA server is generating your results - this page will automatically
refresh every 1 seconds.<P>
Thank you for your patience!<P>
Server Progress:<P>
<blockquote>
<font size=2>Elapsed Time:</font> <code>19.00 seconds</code><BR>
</blockquote>
Server Information:<P>
<blockquote>
<font size=2>Server load:</font> <code>1.72 (1min)  0.86 (5min)  0.41
(15min)</code><BR>
<font size=2>Server time:</font> <code>Tue Aug  9 15:33:56 2011</code><BR>
</blockquote>
</BODY></HTML>

*And it should be send forward to a result page afterwards.
*I think it is because I got exception error.
##### mechanize._response.httperror_seek_wrapper: HTTP Error refresh: The
HTTP server returned a redirect error that would lead to an infinite loop.
##### The last 30x error message was:
##### OK

and I stop after this exception error.
Is there a way to prolong the allowance for refreshing?



Troels Emtekær Linnet
Karl-Liebknecht-Straße 53, 2 RE <http://maps.google.dk/>
04107 Leipzig, Tyskland
Mobil: +49 1577-8944752
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110809/52819857/attachment.html>


More information about the Tutor mailing list