参数必须是一个字节实例WSGI Python3

时间:2016-03-09 08:46:13

标签: python-3.x wsgi pysimplesoap wsgiref

我在Python3中有一个带有pysimplesoap的soap服务器。我不知道,因为我得到了nexts erros。

代码

   from wsgiref.simple_server import make_server
    application = WSGISOAPHandler(dispatcher)
    wsgid = make_server('', 8008, application)
    wsgid.serve_forever()

错误

Traceback (most recent call last):
  File "/usr/lib/python3.4/wsgiref/handlers.py", line 138, in run
    self.finish_response()
  File "/usr/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/lib/python3.4/wsgiref/handlers.py", line 266, in write
    "write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance

4 个答案:

答案 0 :(得分:3)

这是因为WSGI是针对python 2编写的,因此在python3中使用它会遇到一些麻烦。 如果您不想像第一个答案那样更改库的行为,解决方法是对所有文本数据进行编码,例如:

def application(environ,start_response):
    response_body = 'Hello World'
    return [response_body.encode()]

答案 1 :(得分:1)

Wsgi框架是基于Python 2构建的。因此,如果程序中包含不包含Python 3依赖项的内容,请使用Python 2运行该应用程序。

答案 2 :(得分:0)

+++ pysimplesoap/server.py  

                     e['name'] = k
                     if array:
                         e[:] = {'minOccurs': "0", 'maxOccurs': "unbounded"}
-                    if v in TYPE_MAP.keys():
-                        t = 'xsd:%s' % TYPE_MAP[v]
-                    elif v is None:
+
+                    # check list and dict first to avoid
+                    # TypeError: unhashable type: 'list' or
+                    # TypeError: unhashable type: 'dict'
+                    if v is None:
                         t = 'xsd:anyType'
                     elif isinstance(v, list):
                         n = "ArrayOf%s%s" % (name, k)
                         n = "%s%s" % (name, k)
                         parse_element(n, v.items(), complex=True)
                         t = "tns:%s" % n
+                    elif v in TYPE_MAP.keys():
+                        t = 'xsd:%s' % TYPE_MAP[v]
                     else:
                         raise TypeError("unknonw type v for marshalling" % str(v))
                     e.add_attribute('type', t)

答案 3 :(得分:0)

在handlers.py第180行

self.write(data.encode())而不是self.write(data)

相关问题