1
1
#!/usr/bin/env python
2
2
3
+ from __future__ import print_function
3
4
import sys
4
5
5
6
import mechanize
17
18
# backwards_compat=False)
18
19
# f.close()
19
20
form = forms [0 ]
20
- print form # very useful!
21
+ print ( form ) # very useful!
21
22
22
23
# A 'control' is a graphical HTML form widget: a text entry box, a
23
24
# dropdown 'select' list, a checkbox, etc.
37
38
# Add files to FILE controls with .add_file(). Only call this multiple
38
39
# times if the server is expecting multiple files.
39
40
# add a file, default value for MIME type, no filename sent to server
40
- form .add_file (open ("data.dat" ))
41
+ form .add_file (open ("data.dat" , 'rb' ))
41
42
# add a second file, explicitly giving MIME type, and telling the server
42
43
# what the filename is
43
- form .add_file (open ("data.txt" ), "text/plain" , "data.txt" )
44
+ form .add_file (open ("data.txt" , 'rb' ), "text/plain" , "data.txt" )
44
45
45
46
# All Controls may be disabled (equivalent of greyed-out in browser)...
46
47
control = form .find_control ("comments" )
47
- print control .disabled
48
+ print ( control .disabled )
48
49
# ...or readonly
49
- print control .readonly
50
+ print ( control .readonly )
50
51
# readonly and disabled attributes can be assigned to
51
52
control .disabled = False
52
53
# convenience method, used here to make all controls writable (unless
80
81
81
82
# You can get the Control instances from inside the form...
82
83
control = form .find_control ("cheeses" , type = "select" )
83
- print control .name , control .value , control .type
84
+ print ( control .name , control .value , control .type )
84
85
control .value = ["mascarpone" , "curd" ]
85
86
# ...and the Item instances from inside the Control
86
87
item = control .get ("curd" )
87
- print item .name , item .selected , item .id , item .attrs
88
+ print ( item .name , item .selected , item .id , item .attrs )
88
89
item .selected = False
89
90
90
91
# Controls may be referred to by label:
@@ -126,7 +127,7 @@ def control_has_caerphilly(control):
126
127
127
128
# Control.items is a list of all Item instances in the control
128
129
for item in form .find_control ("cheeses" ).items :
129
- print item .name
130
+ print ( item .name )
130
131
131
132
# To remove items from a list control, remove it from .items:
132
133
cheeses = form .find_control ("cheeses" )
@@ -151,13 +152,13 @@ def control_has_caerphilly(control):
151
152
# Which items are present, selected, and successful?
152
153
# is the "parmesan" item of the "cheeses" control successful (selected
153
154
# and not disabled)?
154
- print "parmesan" in form ["cheeses" ]
155
+ print ( "parmesan" in form ["cheeses" ])
155
156
# is the "parmesan" item of the "cheeses" control selected?
156
- print "parmesan" in [
157
- item .name for item in form .find_control ("cheeses" ).items if item .selected ]
157
+ print ( "parmesan" in [
158
+ item .name for item in form .find_control ("cheeses" ).items if item .selected ])
158
159
# does cheeses control have a "caerphilly" item?
159
- print "caerphilly" in [
160
- item .name for item in form .find_control ("cheeses" ).items ]
160
+ print ( "caerphilly" in [
161
+ item .name for item in form .find_control ("cheeses" ).items ])
161
162
162
163
# Sometimes one wants to set or clear individual items in a list, rather
163
164
# than setting the whole .value:
@@ -179,21 +180,21 @@ def control_has_caerphilly(control):
179
180
# Items may be disabled (selecting or de-selecting a disabled item is
180
181
# not allowed):
181
182
control = form .find_control ("cheeses" )
182
- print control .get ("emmenthal" ).disabled
183
+ print ( control .get ("emmenthal" ).disabled )
183
184
control .get ("emmenthal" ).disabled = True
184
185
# enable all items in control
185
186
control .set_all_items_disabled (False )
186
187
187
188
request2 = form .click () # mechanize.Request object
188
189
try :
189
190
response2 = mechanize .urlopen (request2 )
190
- except mechanize .HTTPError , response2 :
191
+ except mechanize .HTTPError as response2 :
191
192
pass
192
193
193
- print response2 .geturl ()
194
+ print ( response2 .geturl () )
194
195
# headers
195
- for name , value in response2 .info ().items ():
196
+ for name , value in list ( response2 .info ().items () ):
196
197
if name != "date" :
197
- print "%s: %s" % (name .title (), value )
198
- print response2 .read () # body
198
+ print ( "%s: %s" % (name .title (), value ) )
199
+ print ( response2 .read () ) # body
199
200
response2 .close ()
0 commit comments