在Python字符串中使用冒号

1 投票
2 回答
1610 浏览
提问于 2025-04-16 06:31

我正在使用 Python 2.6.5 开发一个谷歌应用引擎的应用程序——我对 Python 还不太熟悉,但我正在学习。

我想把一个网址放进一个字符串里,所以我写了这个变量: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"
...

example logout.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)

任何帮助都会很感激。

谢谢。

2 个回答

0

问题出在调用打印或重定向之后的 sys.exit() 这行代码。

7

如果我没记错的话,GAE(谷歌应用引擎)是用WSGI这个东西的,你不能直接打印东西,而是应该返回一个合适的HTTP响应对象(这和PHP不一样)。

我想如果你用火狐浏览器加上火狐开发者工具,查看网络部分的头信息,你会发现浏览器把http:当作一个HTTP头,值是"//domain.name"。

补充一下,难道你不应该用"self.response.out.write"而不是"print"吗?

撰写回答