started with Python 3.4 and asyncio support

This commit is contained in:
Tobias Oberstein 2013-12-21 23:49:47 +01:00
parent 25ca03376f
commit 89fe4a46e8
9 changed files with 307 additions and 17 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
/autobahn/autobahn.egg-info*/
doc/_build/*
*.sublime-workspace
__pycache__

View File

@ -16,23 +16,26 @@
##
###############################################################################
from _version import __version__
version = __version__ # backward compat.
# from _version import __version__
# version = __version__ # backward compat.
import util
import choosereactor
import useragent
import flashpolicy
import httpstatus
import utf8validator
import xormasker
import compress
import websocket
# import util
# import choosereactor
# import useragent
# import flashpolicy
# import httpstatus
# import utf8validator
# import xormasker
# import compress
# import websocket
## disable import, since it leads to reactor import
## https://twistedmatrix.com/trac/ticket/6849
#import resource
# ## disable import, since it leads to reactor import
# ## https://twistedmatrix.com/trac/ticket/6849
# #import resource
import prefixmap
import wamp
import wamp2
# import prefixmap
# import wamp
# import wamp2
#import twisted
#import asyncio

View File

@ -0,0 +1,52 @@
###############################################################################
##
## Copyright 2011-2013 Tavendo GmbH
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
###############################################################################
import asyncio
from asyncio import Protocol
class AdapterProtocol(Protocol):
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
print('connection from {}'.format(peername))
self._protocol.transport = transport
def data_received(self, data):
self._protocol.data_received(data)
#print('data received: {}'.format(data.decode()))
#self.transport.write(data)
#self.transport.close()
class AdapterFactory:
def __init__(self, factory, loop = None):
self._factory = factory
if loop is None:
loop = asyncio.get_event_loop()
self._loop = loop
def __call__(self):
proto = AdapterProtocol()
proto.factory = self
proto._protocol = self._factory()
print("asyncio protocol created")
return proto

View File

@ -0,0 +1,53 @@
###############################################################################
##
## Copyright 2011-2013 Tavendo GmbH
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
###############################################################################
from __future__ import absolute_import
from twisted.internet.protocol import Protocol, Factory
class AdapterProtocol(Protocol):
def connectionMade(self):
peername = str(self.transport.getPeer())
print('connection from {}'.format(peername))
self._protocol.transport = self.transport
def dataReceived(self, data):
self._protocol.data_received(data)
#print('data received: {}'.format(data.decode()))
#self.transport.write(data)
#self.transport.loseConnection()
class AdapterFactory(Factory):
def __init__(self, factory, reactor = None):
self._factory = factory
## lazy import to avoid reactor install upon module import
if reactor is None:
from twisted.internet import reactor
self._reactor = reactor
def buildProtocol(self, addr):
proto = AdapterProtocol()
proto.factory = self
proto._protocol = self._factory()
print("twisted protocol created")
return proto

View File

@ -0,0 +1,19 @@
###############################################################################
##
## Copyright 2013 Tavendo GmbH
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
###############################################################################
#import protocol

View File

@ -0,0 +1,39 @@
###############################################################################
##
## Copyright 2011-2013 Tavendo GmbH
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
###############################################################################
class WebSocketServerProtocol:
def data_received(self, data):
print('data received: {}'.format(data.decode()))
self.onMessage(data, False)
def onMessage(self, payload, isBinary):
pass
def sendMessage(self, payload, isBinary = False):
self.transport.write(payload)
class WebSocketServerFactory:
protocol = WebSocketServerProtocol
def __call__(self):
proto = self.protocol()
proto.factory = self
return proto

View File

@ -0,0 +1,8 @@
server1_asyncio:
PYTHONPATH=../../autobahn python3 server.py asyncio
server1_twisted:
PYTHONPATH=../../autobahn python server.py twisted
client1_asyncio:
PYTHONPATH=../../autobahn python3 client.py

View File

@ -0,0 +1,40 @@
###############################################################################
##
## Copyright 2011-2013 Tavendo GmbH
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
###############################################################################
import asyncio
class EchoClient(asyncio.Protocol):
message = 'This is the message. It will be echoed.'
def connection_made(self, transport):
transport.write(self.message.encode())
print('data sent: {}'.format(self.message))
def data_received(self, data):
print('data received: {}'.format(data.decode()))
def connection_lost(self, exc):
print('server closed the connection')
asyncio.get_event_loop().stop()
loop = asyncio.get_event_loop()
coro = loop.create_connection(EchoClient, '127.0.0.1', 8888)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()

View File

@ -0,0 +1,75 @@
###############################################################################
##
## Copyright 2011-2013 Tavendo GmbH
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
###############################################################################
from autobahn.websocket2.protocol import WebSocketServerProtocol, WebSocketServerFactory
class MyServerProtocol(WebSocketServerProtocol):
def onMessage(self, payload, isBinary):
print("message received")
self.sendMessage(payload)
class MyServerFactory(WebSocketServerFactory):
protocol = MyServerProtocol
def run_test_asyncio(factory):
import asyncio
from autobahn.asyncio import AdapterFactory
loop = asyncio.get_event_loop()
coro = loop.create_server(AdapterFactory(factory), '127.0.0.1', 8888)
server = loop.run_until_complete(coro)
print('serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
print("exit")
finally:
server.close()
loop.close()
def run_test_twisted(factory):
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor
from autobahn.twisted import AdapterFactory
endpoint = TCP4ServerEndpoint(reactor, 8888)
endpoint.listen(AdapterFactory(factory))
reactor.run()
if __name__ == '__main__':
import sys
factory = MyServerFactory()
if sys.argv[1] == "asyncio":
run_test_asyncio(factory)
elif sys.argv[1] == "twisted":
run_test_twisted(factory)
else:
raise Exception("no such variant")