Urllib conundrum

Werner Schiendl ws-news at gmx.at
Thu Jan 31 04:47:25 EST 2002


Hi,

If it works with the proxy, but not without it, I guess this proxy changes
some subtleties in the request or responses passed.

I'd recommend to use some network monitor (e. g. EtherReal, which is free
and works fine) to find out what happens.
You can compare the packets transfered for both cases then.

hth
Werner

"Rick Kaye" <rick at kaye.to> wrote in message
news:xr768.14279$B94.2456818 at news02.optonline.net...
> I wanted to write a Python program to retrieve and log my IP address from
my
> Linksys Cable router. The router interface is available through http. To
get
> the IP address manually, you access the router with a browser. However,
the
> router asks for a username and password before letting you get to the
> interface. At first, I thought I would need to program the exchange of
> authentication information. To listen in on how the browser was exchanging
> this information, I installed the PC Magazine utility "Cookie Cop II"
> (http://www.pcmag.com) which acts as a proxy and lets you listen in the
> exchange between the browser and the outside world. But then I discovered
> that Python's urllib module handled user authentication and it seemed all
> was good. I wrote the program that appears below and it worked fine (with
> Cookie Cop II's Cookie Cop function disabled and it's IE proxy function
> enabled).
>
> Now comes my problem. Since I no longer needed Cookie Cop II, I removed
it.
> But then my Python program stopped working. Normal browsing (through
> Internet Explorer) worked fine. And my program could access webpages that
> are not password protected (by changing the url). But the program no
longer
> worked to get to a password protected page.
>
> The error occurs in the line:
> page=router.open("http://192.168.1.1/Status.htm")
> in the getstatuspage function as indicated below. The urllib code seems to
> go into an infinite loop trying and failing to authenticate itself to the
> router until I get a max recursion level error.
>
> Can anyone shed light on why the program below works with the Cookie Cop
II
> proxy but not without it? Or I'd settle for how to make it work without
the
> proxy.
>
> import urllib
> import re
>
> class myURLopener(urllib.FancyURLopener):
>   def prompt_user_passwd(self, host, realm):
>     return 'name', 'passwd'
>
> def getstatuspage():
>   router=myURLopener()
>   page=router.open("http://192.168.1.1/Status.htm")  #The error occurs on
> this line
>   contents=page.readlines()
>   router.close()
>   return contents[0]
>
> def getipaddress(html=None):
>   if not html:
>     return ""
>   match=re.search(r'WAN.+?IP Address.+?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)',
> html)
>   return match.group(1)
>
> def writetolog(IPAddress, logfile="C:\\IPAddress.log"):
>   fp=open(logfile, 'a')
>   fp.write(IPAddress)
>   fp.write("\n")
>   fp.close()
>
> p=getstatuspage()
> if p:
>   i=getipaddress(p)
>   if i:
>     writetolog(i)
>
>
>
>





More information about the Python-list mailing list