3 ms·
Including threading, cache, and rudimentary web front end. Edited for asyncronous feedback. Used as so: http://localhost:9242/http://tinyurl.com/unicycles htt
by zepolen 18y ago
Including 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()