Python Forum
Fetching the port number using asyncio
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fetching the port number using asyncio
#1
Below is the code for multisocket server interacting with client. Port Numbers are pre-assigned as 1000 to 4000 for clients
In the client thread function, writer.get_extra_Info does not fetch the port number(ie.. 1000 to 4000) but displays the random number.
Please advice as how to modify the code below to fetch the correct port Number

import asyncio

async def client_thread(reader, writer):
  while True:
   data = (await reader.read(1024))
   if data:
            msg=data.decode()
            addr,port=writer.get_extra_info("peername")
            print("Message from", addr,port)
            writer.write(data)
            await writer.drain()           
   else:
            writer.close()
            print("Connection Closed with the client",addr,port)
            return
      
async def start_servers(host, port):
 server = await asyncio.start_server(client_thread, host, port)
 await server.serve_forever()

def enable_sockets():
 try:
        host = '10.102.3.16'
        port = 1000
        sockets_number = 4
        loop = asyncio.get_event_loop()
        for i in range(sockets_number):
            loop.create_task(start_servers(host,port*(i+1)))
            print("Connected to Ports:",host,port*(i+1))
        loop.run_forever()
 except Exception as exc:
        print(exc)

enable_sockets()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  fetching exit status hangs in paramiko saisankalpj 3 1,193 Dec-04-2022, 12:21 AM
Last Post: nilamo
  is there a way to mention port range or search for port dynamically with qConnection Creepy 0 1,493 Sep-09-2021, 03:15 PM
Last Post: Creepy
  Fetching data from multiple tables in a single request. swaroop 0 1,906 Jan-09-2021, 04:23 PM
Last Post: swaroop
  Fetching data from Sklearn zaki424160 2 4,128 Jul-09-2019, 05:20 AM
Last Post: ThomasL
  Error while fetching data from PostgreSQL linu 3 4,327 May-13-2019, 02:38 PM
Last Post: rxndy
  Need help to correct my python function for fetching full data! PrateekG 2 2,921 May-27-2018, 06:39 AM
Last Post: PrateekG
  Fetching private ip address from instances of an autoscaling group deepsonune 0 3,278 May-18-2018, 10:32 AM
Last Post: deepsonune
  Delay in fetching kerberos tokens : python gssapi securitycontext unni10286 1 2,630 Apr-30-2018, 04:12 PM
Last Post: micseydel
  Fetching html files from local directories shiva 3 3,441 Mar-20-2018, 05:12 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020