web2py windowslinux互操作性

2024-03-28 20:36:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个在Windows机器上用Web2py框架开发的系统,现在它部署在Linux机器上。在这个系统中,我检查Unicode的输入数据类型,如果不是,则返回错误消息,因为需要Unicode。我将参数作为u'HI'unicode('HELLO')发送给它。这在我的Windows机器上运行,但是现在我已经在Linux上部署了这个系统,我得到了一个错误,因为需要Unicode。如果我遗漏了什么,请告诉我。你知道吗

我在两台机器上都有相同的Python版本(2.7.2)。你知道吗

这就是我检查输入变量类型的方法:

if isinstance(value,getattr(__builtin__,'unicode')) or isinstance(value,unicode)):                    
    pass
else:
    return "error message"

我在我的机器和linux服务器上尝试了以下命令来检查python编码设置。其结果如下:

我的机器-

>>> import sys
>>> print sys.stdout.encoding
cp1252
>>> print sys.stdin.encoding
cp1252
>>> sys.getdefaultencoding()
'ascii'
>>> 

在Linux机器上

>>> import sys
>>> print sys.stdout.encoding
UTF-8
>>> print sys.stdin.encoding
UTF-8
>>> sys.getdefaultencoding()
'ascii'
>>>

我是否需要更改任何机器的编码设置才能解决此问题?你知道吗


Tags: import机器编码valuelinuxwindows系统部署
1条回答
网友
1楼 · 发布于 2024-03-28 20:36:26

如果可能的话,通常使用avoid directly type-checking变量-只要使用它们。这是an example that does it with try-except。你知道吗

在完全丢弃输入之前强制输入是无效的吗?例如,如果您的linux系统正在传递ascii字符串,为什么不直接将它们转换为utf-8呢?你知道吗

try:
    value = value.decode('utf-8')
except (SyntaxError, AttributeError):  # I would probably just do Exception to catch all
    return "error message"

如果这还不能解决问题,那么您是否可以调查一下您在windows和linux上收到的确切输入,并解释为什么强制对您不起作用?你知道吗

相关问题 更多 >