url lib

Chris Rebert clp2 at rebertia.com
Fri Mar 20 14:24:27 EDT 2009


On Fri, Mar 20, 2009 at 10:07 AM, CSUIDL PROGRAMMEr <amjadcsu at gmail.com> wrote:
> Hello
> I am a novice python progammer
>
> I am just writing a script that will automatically write my /etc/
> yum.repos.d/* files in linux.
>
> the problem is i am trying to connect to a secure repo and pass
> username and password to the baseurl .
>
> here is the baseurl
> baseurl=http://USERNAME:PASSWORD@ydl.net/yum/6.1/../../../.
>
> I have tried using the urllib2 in python
>
> the code i tried was
> req=urllib2.Request("http://USERNAME:PASSWord@ydl.net/cell/yum/6.1/
> base)
> try: urllib2.urlopen(req)
> except urllib2.URLError,e:
>
> Where Username:PASSWORD are speciefied by user
>
> The Error i got was
> File "/usr/lib/python2.4/urllib2.py", line 130, in urlopen
>    return _opener.open(url, data)
>  File "/usr/lib/python2.4/urllib2.py", line 358, in open
>    response = self._open(req, data)
>  File "/usr/lib/python2.4/urllib2.py", line 376, in _open
>    '_open', req)
>  File "/usr/lib/python2.4/urllib2.py", line 337, in _call_chain
>    result = func(*args)
>  File "/usr/lib/python2.4/urllib2.py", line 1021, in http_open
>    return self.do_open(httplib.HTTPConnection, req)
>  File "/usr/lib/python2.4/urllib2.py", line 980, in do_open
>    h = http_class(host) # will parse host:port
>  File "/usr/lib/python2.4/httplib.py", line 592, in __init__
>    self._set_hostport(host, port)
>  File "/usr/lib/python2.4/httplib.py", line 604, in _set_hostport
>    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
> httplib.InvalidURL: nonnumeric port: 'PAssword at ydl.net'
>
> How can i connect to the site with password?? Do i have to implement
> the ssl certification??

Not bloody likely since your the URL's protocol is HTTP and not HTTPS.

Python doesn't handle username-password in the URL itself like you're
using. You specify it separately instead.

See the HTTPBasicAuthHandler, HTTPDigestAuthHandler, and
HTTPPasswordMgrWithDefaultRealm classes in urllib2, and the "Use of
Basic HTTP Authentication" example in the urllib2 docs -
http://docs.python.org/library/urllib2.html

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com



More information about the Python-list mailing list