Starting a server

In the Chapter11/example1.py file, let's look at the EchoServerClientProtocol class, as follows:

# Chapter11/example1.py

import asyncio

class EchoServerClientProtocol(asyncio.Protocol):
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
print('Connection from {}'.format(peername))
self.transport = transport

def data_received(self, data):
message = data.decode()
print('Data received: {!r}'.format(message))

Here, our EchoServerClientProtocol class is a subclass of asyncio.Protocol. As we discussed earlier, inside of this class, we need to implement the connection_made(transport) and data_received(data) methods. In the connection_made() method, we simply obtain the address of the connected system via the get_extra_info() method (with the 'peername' argument), print a message out with that information, and finally store the transport object in an attribute of the class. In order to print out a similar message in the data_received() method, again we use the decode() method to obtain a string object from byte data.

Let us move on to the main program of our script, as follows:

# Chapter11/example1.py

loop = asyncio.get_event_loop()
coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)

# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass

# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

We are using the familiar asyncio.get_event_loop() function to create an event loop for our asynchronous program. Then, we create a server for our communication by having that event loop call the create_server() method; this method takes in a subclass from the asyncio.Protocol class, an address for our server (in this case, it is our local host: 127.0.0.1), and finally, a port for that address (typically, 8888).

Note that this method does not create the server itself; it only initiates the process of creating the server asynchronously, and returns a coroutine that will finish the process. For this reason, we need to store the returned coroutine from the method in a variable (coro, in our case) and have our event loop run that coroutine. After printing out a message using the sockets attribute of our server object, we will run the event loop forever, in order to keep the server running, except for the case of a KeyboardInterrupt exception being invoked.

Finally, at the end of our program, we will handle the house cleaning portion of the script, which is closing the server gracefully. This is typically done by having the server object call the close() method (to initiate the closing process of the server) and using the event loop to run the wait_closed() method on the server object, to make sure that the server is properly closed. Finally, we close the event loop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.225