python ladon webservice的方法中包含中文无法正常生成soap的解决办法

python ladon webservice的方法中包含中文无法正常生成soap的解决办法,第1张

概述当在python3中使用Ladon库发布webservice的时候,如果定义的方法中包含中文字符(参数说明除外),例如下面代码,到导致在web端http://localhost:port中出现无法查看soap的description1classAccountManager(object):2@ladonize(str,str,rtype=str)3defgetUsername(self,

当在python3中使用Ladon库发布webservice的时候,如果定义的方法中包含中文字符(参数说明除外),例如下面代码,到导致在web端http://localhost:port中出现无法查看soap的description

1 class AccountManager(object):2     @ladonize(str, str, rtype=str)3     def getUsername(self, userID, mailAddr='None'):4         ''' 这是中文描述字符'''

 然后点击下图中的连接时,无法正常显示WSDL内容。

 

 

主要的原因是wsgi_application.py文件中的一个BUG导致,这个BUG发生在python3版本上。

在该文件中有如下代码:

1         if not hasattr(output, 'read'):2             # not file-like object3             content_length = str(len(output))

这里的content_length是计算header的Content-Length的值,output是字符串类型进行计算

而在文件的最后,有这样的代码:

1         if sys.version_info[0] >= 3:2             # Python 3 support3             if type(output) == str:4                 output = bytes(output, charset)5         return [output]

这里的output被转换成了bytes类型。而在output中包含中文的情况下,对于str类型的len(output)和对于bytes类型的len(output)的结果是不一样的,str类型的结果会比bytes类型的结果小,每个中文字符会小一个字节的长度,就导致最后生成的wsdl的内容被截取而出现语法错误,进而无法通过浏览器完整显示出来。

解决办法就是将上面的第一段代码修改如下:

1         if not hasattr(output, 'read'):2             # not file-like object3             if sys.version_info[0] >= 3:4             # Python 3 support5                 if type(output) == str:6                     output = bytes(output, charset)7             content_length = str(len(output))

问题将得到解决。

 

总结

以上是内存溢出为你收集整理的python ladon webservice的方法中包含中文无法正常生成soap的解决办法全部内容,希望文章能够帮你解决python ladon webservice的方法中包含中文无法正常生成soap的解决办法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1188780.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存