Bug in urllib?

Christoph Zwerschke cito at online.de
Sun Oct 15 05:00:44 CEST 2006


goyatlah wrote:
> urllib.url2pathname("http://127.0.0.1:1030/js.cgi?pca&r=12181")
> gives IOError : Bad Url, only coz of the :1030 which should be
> accurate portnumber. Is it something I did wrong, or a bug. And what
> to do to avoid this (except rewriting url2pathname)?

 >>> help(urllib.url2pathname)

url2pathname(url)
     OS-specific conversion from a relative URL of the 'file' scheme
     to a file system path; not recommended for general use.

So first, you must use a URL of the 'file' scheme, not of the 'http' 
scheme. This is something like:

'file://localhost/C|/srv/cgi.bin/js.cgi' or 'file:///C|/srv/cgi.bin/js.cgi

Second, it must be a "relative URL" which is a bit mistakable here. What 
is actually meant is that you must not prepend it with 'file:'.

So you have to pass

'//localhost/C|/srv/cgi.bin/js.cgi' or
'///C|/srv/cgi.bin/js.cgi'

On Windows, this will give you:

 >>> urllib.url2pathname('//localhost/C|/srv/cgi.bin/js.cgi')
C:\srv\cgi.bin\js.cgi

The docs at http://docs.python.org/lib/module-urllib.html say basically 
the same, you need to pass the *path component* of an URL, not the 
complete URL.

-- Christoph



More information about the Python-list mailing list