[Tutor] getting a webpage via python
Kent Johnson
kent37 at tds.net
Tue Mar 8 17:50:12 CET 2005
Paul Tremblay wrote:
> Is there a simple way to get a web page with python? I have done no
> network programming with python before.
>
> My router (a Linksys 54G model) stores the IP/MAC addresss in a web
> page. There is no way for me to access them except through the web.
>
> Righ now, I am using this code:
>
> command = 'lynx -reload -auth %s:%s -source http://%s/DHCPTable.asp > %s' % (
> self.__log_name, self.__log_password, self.__router_address, temp_out1)
> (exit_status, out_text) = commands.getstatusoutput(command)
You can use urllib2 to do this. It is a little work to set it up to use Basic authentication. Here
is an example (slightly modified from the urllib2 example page):
import urllib2
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password('realm', '127.0.0.1', 'username', 'password')
opener = urllib2.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib2.install_opener(opener)
print urllib2.urlopen('http://127.0.0.1/my/protected/page.html').read()
Kent
More information about the Tutor
mailing list