I'm trying to simulate a network using asychronious TCP servers and sockets. I used an example from documentation as a starting point for my task. Here's the code of my server class:
import asyncio
import socket
import Node
class ServerProtocol(asyncio.Protocol):
def __init__(self, hostNode):
self.hostNode = hostNode
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
print('Connection from {}'.format(peername))
self.transport = transport
def data_received(self, data):
print('Data received: {!r}'.format(data))
self.hostNode.processIncomingMessage(data)
class NodeServer:
def __init__(self, hostNode):
self.loop = asyncio.get_event_loop()
self.hostNode = hostNode
def startListening(self):
self.coro = self.loop.create_server(ServerProtocol(self.hostNode), '', 0, family=socket.AF_INET)
server = self.loop.run_until_complete(self.coro)
def getPortNumber(self):
print(self.coro.sockets)
portNumber = self.coro.sockets[0].getpeername()[1]
print(portNumber)
return portNumber
I call create_server function with params '', 0 and family=socket.AF_INET because I need to establish IPv4 version and the OS must give appropriate random port to listen. In the code below I'm trying to get a number of port.
Documentation says, create_server function returns Server object, and sockets can be retrieved from sockets attribute.
But when I run the code, I get following error:
AttributeError: 'generator' object has no attribute 'sockets'
It happens when executing self.coro.sockets
So, that's the issue.
Could someone help me with this, please?
Many thanks.