我正在使用 python 2.6.5 为谷歌应用引擎开发应用程序 - 我不太熟悉 python,但我正在学习。
我正在尝试将 url 放入字符串中,因此 variable = "string http://domain.name "
然后我将字符串打印出来。问题是,如果冒号(在 http 之后)在字符串中,我不会得到任何输出,我也不知道为什么。
我试过用以下方法转义字符串:
- """http://domain.name"""
- r"http://domain.name"
- "http\://domain.name"
- "http\://domain.name"
- "http\\://domain.name"
- "http://domain.name"
它们似乎都不起作用,我不确定还能尝试什么
上下文是这样的
variables.py 是:
...
HOST_URL = "http://domain.name"
...
示例注销.py
import variables
import sys
...
class Logout(webapp.RequestHandler):
""" RequestHandler for when a user wishes to logout from the system."""
def post(self):
self.get()
def get(self):
print(variables.HOST_URL)
print('hi')
self.redirect(variables.HOST_URL)
sys.exit()
或
在文件 functions.py 中
import variables
import sys
...
def sendhome(requesthandler)
print 'go to '+variables.HOST_URL
requesthandler.redirect(variables.HOST_URL)
sys.exit()
从如下上下文中调用:
from functions import sendhome
...
class Logout(webapp.RequestHandler):
""" RequestHandler for when a user wishes to logout from the system."""
def post(self):
self.get()
def get(self):
sendhome(self)
任何帮助将不胜感激
谢谢
最佳答案
如果我没记错的话,GAE 使用 WSGI,你不只是打印东西,你应该返回一个正确的 HTTP 响应对象(它不是 PHP)。
我想如果您使用 firefox+firebug 访问该页面并查看 network->header,您会看到浏览器将 http: 作为值为“//domain.name”的 HTTP header 。
已编辑:顺便说一句,你不应该使用“self.response.out.write”而不是“print”吗?
关于python - 在 python 中的字符串中使用冒号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4092159/