不能在类字节对象SOAP服务器上使用字符串模式

时间:2016-02-02 15:45:13

标签: python soap

当我整合" WSGISOAPHandler" SOAP服务器不起作用。有任何想法吗? 当客户端调用服务器时,服务器返回"不能在类字节对象上使用字符串模式"

if __name__ == "__main__":

    def ota_vehlocsearch(respuesta):
        return "respuesta"

    dispatcher = SoapDispatcher(
        name='soap',
        location = "http://localhost:8008/",
        action = 'http://localhost:8008/', # SOAPAction
        namespace = "http://www.opentravel.org/OTA/2003/05", prefix="ns0",
        trace = True,
        ns = True)

    dispatcher.register_function('OTA_VehLocSearch', ota_vehlocsearch,
        returns={'Result': str},
        args={'call': str})

    print("Starting wsgi server...")
    from wsgiref.simple_server import make_server
    application = WSGISOAPHandler(dispatcher)
    wsgid = make_server('localhost', 8008, application)
    wsgid.serve_forever()

1 个答案:

答案 0 :(得分:0)

这个问题从未得到答案。我也从未使WSGISOAPHandler(dispatcher)正常工作,因为它总是会在第297行的assert type(data) is bytes, "write() argument must be a bytes instance"中抛出/usr/lib/python3.8/wsgiref/handlers.py。 由于wsgiref.simple_server import make_server来自python2,它对编码的处理方式不同,所以我决定改为使用from http.server import HTTPServer来避免所有这些编码问题。

这是我使用客户端和服务器制作的示例:

服务器:

from pysimplesoap.server import SoapDispatcher, SOAPHandler
from http.server import HTTPServer

def cityweatherreport(city_name):
    #Some Code here to fetch weather info:
    weather_report = f"""Weather in {city_name} is """
    print('weather_report: ', weather_report)
    return{'weather_report': weather_report}

def server_accepting_soap_requests():
    dispatcher = SoapDispatcher(
    name="WeatherServer",
    location="http://127.0.0.1:8050/",
    action='http://127.0.0.1:8050/', # SOAPAction
    namespace="http://example.com/pysimplesoapsamle/",
    prefix="ns0",
    trace = True,
    ns = True)
    dispatcher.register_function('CityWeatherReport', cityweatherreport,
        returns={'weather_report': str},
        args={'city_name': str})
    print('starting server')
    httpd = HTTPServer(("", 8050), SOAPHandler)
    httpd.dispatcher = dispatcher
    httpd.serve_forever()

def main():
    server_accepting_soap_requests()

if __name__ == '__main__':
    main()

客户

from pysimplesoap.client import SoapClient

def test_weather_conversion_service():
    client = SoapClient(
    location="http://localhost:8050/",
    action='http://localhost:8050/',  # SOAPAction
    namespace="http://example.com/sample.wsdl",
    soap_ns='soap',
    trace=True,
    ns="ns0",
    )
    response = client.CityWeatherReport(city_name="Berlin")
    result = response.weather_report
    print('result: ', result)

def main():
    test_weather_conversion_service()

if __name__ == '__main__':
    main()

要在专用计算机上远程运行它,我建议使用类似屏幕的工具:screen -d -m -S <username> python3 <pythonfile.py>