-
Notifications
You must be signed in to change notification settings - Fork 6
PlaygroundConverstionQuickstart
Playground is designed to work as a drop in replacement for network communications built using Asyncio's create_server
and create_connection
methods.
Playground also offers a create_server
and create_connection
method. They work exactly the same, except that they use playground addresses:
Old code:
class MyProtocol(asyncio.Protocol):
def connection_made(self, transport):
# stuff
def data_received(self, data);
# more stuff
loop = asyncio.get_event_loop()
loop.create_server(lambda: MyProtocol, HOST, PORT)
New code:
class MyProtocol(asyncio.Protocol):
def connection_made(self, transport):
# stuff
def data_received(self, data);
# more stuff
playground.create_server(lambda: MyProtocol, HOST, PORT)
Hopefully, you can see that this is pretty easy. It's a one-line difference. Also, HOST will need to be a playground address instead of an Internet host or Internet address
As we get a little farther into the semester, there will be different ways of interacting with Playground over different kinds of protocols. Suppose, for example, that you create a new protocol stack to handle communications on Playground and you name it "my_netsec_stack". You can tell Playground to use that stack with typical URI prefixing:
HOST = "my_netsec_stack://20191.1.2.3"
PORT = 700
playground.create_server(lambda: MyProtocol, HOST, PORT)
Or you can give an explicit "family" argument.
HOST = "20191.1.2.3"
PORT = 700
playground.create_server(lambda: MyProtocol, HOST, PORT, family="my_netsec_stack")
You DO NOT need to worry about this for the early assignments. So just put a book-mark here for when we get into stacks later in the semester.