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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 2,357 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 2,036 Sep-09-2021, 03:15 PM
Last Post: Creepy
  Fetching data from multiple tables in a single request. swaroop 0 2,399 Jan-09-2021, 04:23 PM
Last Post: swaroop
  Fetching data from Sklearn zaki424160 2 4,886 Jul-09-2019, 05:20 AM
Last Post: ThomasL
  Error while fetching data from PostgreSQL linu 3 5,563 May-13-2019, 02:38 PM
Last Post: rxndy
  Need help to correct my python function for fetching full data! PrateekG 2 3,605 May-27-2018, 06:39 AM
Last Post: PrateekG
  Fetching private ip address from instances of an autoscaling group deepsonune 0 3,762 May-18-2018, 10:32 AM
Last Post: deepsonune
  Delay in fetching kerberos tokens : python gssapi securitycontext unni10286 1 3,197 Apr-30-2018, 04:12 PM
Last Post: micseydel
  Fetching html files from local directories shiva 3 4,268 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