Python Socket programming: printing source address and source destination

Viewed 285

I'm currently studying socket.io and I wonder if I could print the 4 tuples in tcp connection (Source port, address, destination port, address). I am able to print the destination port and address by after 'accept()', but struggled with the source part. Here are my codes.

Server:

import socket

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket successfully created")

serverPort = 39557

serverSocket.bind(('', serverPort))
print ("socket binded to %s" %(serverPort))

serverSocket.listen(5)
print ("socket is listening")       


while True:
    connectionSocket, addr = serverSocket.accept()
    #this will print out destination address & port
    print ('Destination address & port:', addr ) 
    sentence='thank you for connecting'
    connectionSocket.send(sentence.encode())
    connectionSocket.close()
    break

Client:

import socket
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverName = ''
serverPort = 39557
clientSocket.connect((serverName, serverPort))
sentence = clientSocket.recv(1024).decode()
print ("from server:", sentence)
clientSocket.close()
0 Answers
Related