4 ms·
In modern Python: def expand_url(url): return urllib2.urlopen(url).geturl()
by andr 18y ago
In modern Python:
def expand_url(url):
return urllib2.urlopen(url).geturl()
- jrockway 18y agoSure, now do that asynchronously (including DNS lookups) and add a web frontend. Then you will have something close to what the article describes. The equivalent to what you describe is: LWP::UserAgent->new->get($url)->request->uri
- andr 18y agoAgreed, it is not the same, but I'm saying if I needed to resolve a shortened URL, I would do it myself instead of sending an HTTP request to you so that you can send an HTTP request on my behalf. At any rate, using your service would be slower because of the extra roundtrip, and more unreliable. However, I appreciate your post for enlightening what modern Perl looks like. After a quick skim I thought I was looking at Rails.
- jrockway 18y agoOne advantage of my "service" is that if the URL redirector goes down, you still have a chance at getting my cached copy. The point of the article is not the service, but rather what the code looks like. Returning "Hello world" is almost as instructive, but not quite, so I chose a dumb example instead.
- systems 18y agowhich is also most definitely not just about having TryCatch and Class syntax It's also about Kiouku , POE and Moose's Modern Object system. And how Perl5 is still evolving and being actively modernized.
- thwarted 18y agoThis perl is so modern, it's looked like this for over a decade. It's only gotten more featureful since then.
- zepolen 18y agoIncluding threading, cache, and rudimentary web front end. Edited for asyncronous feedback. Used as so: http://localhost:9242/http://tinyurl.com/unicycles http://localhost:9242/http://tinyurl.com/unicycles import urllib2, cherrypy.wsgiserver cache = {} def app(environ, start_response): start_response('200 OK', [('Content-type','text/plain')]) url = environ['PATH_INFO'].lstrip('/') yield 'Looking up: ' + url + '...' try: result = cache.get(url) if result is None: result = urllib2.urlopen(url).geturl() cache[url] = result except urllib2.HTTPError: result = "URL Doesn't exist" except ValueError: result = "Invalid Url" yield result cherrypy.wsgiserver.CherryPyWSGIServer(('127.0.0.1', 9242), app).start()