-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] SOAP REST Python- Spyne

, 27 2017 . 18:13 +

Spyne


Python- Spyne.
Spyne , -, SOAP-. Spyne, soaplib. , .

Spyne -, SOAP, JSON, YAML, mod_wsgi Apache. , , , apache.

1. SOAP-


-, . - , Yandex-translator, . XML-. XML-.

API-, , . , .

.

: pytz, spyne, yandex_translate. pip.

:

from spyne import Application, rpc, ServiceBase, Unicode
from lxml import etree
from spyne.protocol.soap import Soap11
from spyne.protocol.json import JsonDocument
from spyne.server.wsgi import WsgiApplication
from yandex_translate import YandexTranslate

class Soap(ServiceBase):
    @rpc(Unicode, _returns=Unicode)
    def Insoap(ctx, words):
        print(etree.tostring(ctx.in_document))
        translate = YandexTranslate('trnsl.1.1.201somesymbols')
        tr = translate.translate(words, 'en')
        tr_answer = tr['text'][0]
        return tr_answer
app = Application([Soap], tns='Translator',
                          in_protocol=Soap11(validator='lxml'),
                         out_protocol=Soap11()
application = WsgiApplication(app)
if __name__ == '__main__':
     from wsgiref.simple_server import make_server
    server = make_server('0.0.0.0', 8000, application)
    server.serve_forever()

:

, Soap ServiceBase. "@rpc(Unicode, _returns=Unicode)" (Unicode) ("_returns=Unicode"). .. Insoap ctx words. ctx , . print(etree.tostring(ctx.in_document)) xml-, , . . , - xml- . xml- Spyne. Burak Arslan ( Spyne) lxml. . - - .

app -: Application([Soap] , ( ), in_protocol out_protocol , SOAP v1.1.

application = WsgiApplication(app) , wsgi.

! application, apache mod_wsgi.
- 8000.

. SoapUI. , SOAP , SoapUI xml-. xml- :

xml-
schemas.xmlsoap.org/soap/envelope xmlns:tran=Translator>







- :

schemas.xmlsoap.org/soap/envelope xmlns:tns=Translator>


Test our app



, ?

2. REST-


, . , -, JSON. ? , Django Rest Framework Flask? ? , ! !

Spyne .

, app :

app = Application([Soap], tns='Translator',
                          in_protocol=JsonDocument(validator='soft'),
                          out_protocol=JsonDocument())

- .

JSON- :

JSON-
{Insoap: {words: -. JSON}}

:

-
test our web service. Use JSON

3.


- apache, - apache mod_wsgi. , . , :

if __name__ == '__main__':
from wsgiref.simple_server import make_server
server = make_server('0.0.0.0', 8000, application)
server.serve_forever()

! - .

P.S. Spyne ( ) , .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/334290/

:  

: [1] []
 

:
: 

: ( )

:

  URL