ritratt
ritratt

Reputation: 1858

Flask app unable to parse suburls

I have created a simple heroku app that takes a url as the argument, reads the url data and displays it in the browser. Here is the code:

import os
from flask import Flask
import urllib2, urlparse

app = Flask(__name__)

@app.route('/<url>')
def getdata(url):
    url.replace('www.','')
    if url.count('http') == 0:
        url = 'http://'+url
    u = urlparse.urlparse(url)
    nurl = u.geturl()
    response = urllib2.urlopen(nurl).read()
    return response

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

When passed a url like google.com it works fine. But when passed something like google.com/images (I know the url returns 503), it throws a 404 error. When I tried reading suburls normally in my python shell it works just fine. Why is this happening and how do I fix it?

Upvotes: 2

Views: 195

Answers (1)

D.Shawley
D.Shawley

Reputation: 59563

Try using the path option in the @route. Something like the following should work:

@app.route('/<path:url>')
def getdata(url):
    ...

Upvotes: 5

Related Questions