0

I'm trying to grab a hold of some data from a GTFS feed. I haven't gotten very far yet and I'm still trying to figure out how to open up and save the contents locally. Right now I have a flask application with the following:

def mta():
    data = urllib2.urlopen('http://addresshere.php?key=keyvaluehere')
    response = data.read()
    print response

@app.route('/')
def index():
    test = mta()
    return render_template("index.html",
        test = test,
        title = "Home")

When I start up the server, it shows up in the console instead of my browser and I get a message that says "None" in the index.html template.

I did a quick test in PHP using get_file_contents() and actually pulled info, albeit, it looked like gibberish to me. Either way, I'm not sure why "None" is showing up in my template. The terminal is displaying the following as soon as I start up the server (which is similar to what I got using PHP)

11!??????"L15S(?>
11!??????"L16S(?>
11!??????"L17S(?>
11!??????"L19S(?>
11!??????"L20S(?>

On another note, should I set up the mta function as a separate module in the application and import it into the view?

user2989731
  • 1,299
  • 3
  • 17
  • 33

2 Answers2

1

Can you try with this?

def mta():
    data = urllib2.urlopen('http://addresshere.php?key=keyvaluehere')
    response = data.read()
    print response
    return response
est
  • 11,429
  • 14
  • 70
  • 118
  • @Namalak Thanks for the remarks but please don't tamper with the code if you don't know what you are doing. – est Jul 07 '14 at 03:25
  • Ah! yes my bad. I didn't look them closely, so thought indentation issue at the first sight. Apology J. – ironwood Jul 07 '14 at 05:25
0

Your mta function prints the result instead of returning it, so test gets the default of None for functions which don't have a return value.

def mta():
    data = urllib2.urlopen('http://addresshere.php?key=keyvaluehere')
    response = data.read()
    print response # should be return response
Peter Gibson
  • 19,086
  • 7
  • 60
  • 64
  • Switching to return response gives a 500 internal server error – user2989731 Jul 07 '14 at 03:06
  • @user2989731 what is the error shown when you run in debug mode (`app.run(debug=True)`)? Are you using a real url? – Peter Gibson Jul 07 '14 at 03:12
  • It doesn't specify, it just gives a general internal server error. I can however modify it slightly to read `for line in data: return line` to get it to load again, but it returns "None" just like the original `print` statement – user2989731 Jul 07 '14 at 03:18
  • @user2989731 when you run in debug mode, it should display an error page such as in the screen shot here http://flask.pocoo.org/docs/quickstart/#debug-mode – Peter Gibson Jul 07 '14 at 03:42