Not logged in
Log in now
Create an account
Subscribe to LWN
LWN.net Weekly Edition for May 16, 2013
A look at the PyPy 2.0 release
PostgreSQL 9.3 beta: Federated databases and more
LWN.net Weekly Edition for May 9, 2013
(Nearly) full tickless operation in 3.10
For parsing urls though it's simple. From within ipython shell:
In : from urlparse import urlparse
In : url = urlparse('http://lwn.net/Articles/309375/')
In : print url.hostname
In : print url.path
If specified in the string it'll do port, username, and passwords and whatnot.
What is to replace Perl then?
Posted Dec 3, 2008 23:38 UTC (Wed) by jwb (guest, #15467)
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib2 import urlopen
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "/usr/lib/python2.5/urllib2.py", line 381, in open
response = self._open(req, data)
File "/usr/lib/python2.5/urllib2.py", line 399, in _open
File "/usr/lib/python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/usr/lib/python2.5/urllib2.py", line 1107, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.5/urllib2.py", line 1064, in do_open
h = http_class(host) # will parse host:port
File "/usr/lib/python2.5/httplib.py", line 639, in __init__
File "/usr/lib/python2.5/httplib.py", line 651, in _set_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: 'firstname.lastname@example.org'
In Perl the situation is quite satisfactory. The URI module exists and works, and work harmoniously with HTTP::Message and its descendants, which in turn work harmoniously with LWP and WWW::Mechanize and so forth.
Considering python's age and the fact that it has developed coincidentally with the web, you would think that python's web support would be quite mature by now, but it isn't. python's support for basic web operations in quite bad.
Posted Dec 4, 2008 0:15 UTC (Thu) by sbergman27 (guest, #10767)
You mean when you said: "You can do anything you want in python, except the incredibly simple things like parsing URLs."? Yeah, I can see where you might really have meant you have a trivial quibble with the syntax.
Python URL parsing 101, in case anyone is interested:
from urlparse import urlparse
> o = urlparse('http://user:email@example.com:8080/')
Posted Dec 4, 2008 0:17 UTC (Thu) by jwb (guest, #15467)
Posted Dec 4, 2008 2:40 UTC (Thu) by drag (subscriber, #31333)
Posted Dec 4, 2008 3:42 UTC (Thu) by sbergman27 (guest, #10767)
urllib does support this directly:
from urllib import urlopen
> x = urlopen('http://myuser:firstname.lastname@example.org/')
There have, however, been a couple of request nibbles on the issue tracker over the last 4 years or so to add the functionality to urllib2 as well, and no actual opposition to it. Interestingly, there was activity today from a dev saying he was implementing it, noting that it would be trivial to do.
Personally, I think its probably a good idea, but since its just a few lines to handle this case, it doesn't really bother me.
Like I said, this seems something of a cherry-picked example to "prove" that Python's url handling is "not mature". I'm sure that Python and Ruby folks could pick more than a few cherries regarding Perl's problems if they wanted to.
Posted Dec 4, 2008 1:49 UTC (Thu) by jamesh (guest, #1159)
The urllib2 module can perform authentication though, as described in http://docs.python.org/library/urllib2.html#examples
Posted Dec 4, 2008 2:27 UTC (Thu) by jwb (guest, #15467)
Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds