【发布时间】:2015-02-01 18:01:25
【问题描述】:
我正在尝试执行此 API 调用:(名为 wwo.py)
# -*- coding: utf-8 -*-
from tornado.httpclient import AsyncHTTPClient
http_client = AsyncHTTPClient()
url = ''
response = ''
def meteo(q='', key=''):
if len(key) != 29:
print 'Please provide a valid key'
elif q == '':
print 'please provide a valid city name or zip'
global url
url = 'http://api.worldweatheronline.com/free/v2/weather.ashx?q={0}&format=json&key= {1}'.format(q, key)
def handle_request(resp):
global response
if resp.error:
print "Error:", resp.error
else:
response = resp.body
http_client.fetch(url, handle_request)
如果我尝试调用此文件并从普通 python 控制台执行它,我不会得到response,它会保持''。
但是如果我使用 IPython 控制台调用它,或者使用 Spyder 执行它,它会调用response,即使我不初始化它,它也会被调用!
import wwo
wwo.meteo(q='London', key='API_FREE_KEY')
wwo.response # returns '' on normal python console, returns the full response on ipython console
【问题讨论】:
标签: python console ipython tornado spyder