0% found this document useful (0 votes)
5 views4 pages

Ex 3

The document describes the simulation of PING and TRACEROUTE commands using Twisted Python, detailing both server and client implementations for each command. The PING simulation includes a server that responds to 'ping' messages and a client that sends them, while the TRACEROUTE simulation involves a UDP server that responds with hop information. The results demonstrate the successful implementation of these network commands in Python.

Uploaded by

medha8988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Ex 3

The document describes the simulation of PING and TRACEROUTE commands using Twisted Python, detailing both server and client implementations for each command. The PING simulation includes a server that responds to 'ping' messages and a client that sends them, while the TRACEROUTE simulation involves a UDP server that responds with hop information. The results demonstrate the successful implementation of these network commands in Python.

Uploaded by

medha8988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex No.

: 3 Date: / / 2025
Simulation of PING and TRACEROUTE
commands using Twisted Python

Ping Simulation:

Server/Active Port: (ping_server.py)

from twisted.internet import protocol, reactor

class PingServer(protocol.Protocol):
def dataReceived(self, data):
message = data.decode().strip()
print(f"Received: {message}")

if message.lower() == "ping":
self.transport.write("pong\n".encode())
else:
self.transport.write("Unknown command\n".encode())

class PingFactory(protocol.Factory):
def buildProtocol(self, addr):
print(f"New connection from {addr}") # Debug message
return PingServer()

# Start the server on port 8080


reactor.listenTCP(8080, PingFactory())
print("Ping server started. Listening on port 8080...") # Debug message
reactor.run()

Ping Protocol: (ping_client.py)

from twisted.internet import reactor, protocol

class PingClient(protocol.Protocol):
""" Client that sends a 'ping' message and waits for a response. """

def connectionMade(self):
print("Connected to server. Sending 'ping'...")
self.transport.write("ping\n".encode()) # Send "ping" message

def dataReceived(self, data):


print("Server response:", data.decode().strip())
reactor.stop() # Stop after receiving response

class PingClientFactory(protocol.ClientFactory):
""" Factory for creating client connections. """

def buildProtocol(self, addr):


return PingClient()

def clientConnectionFailed(self, connector, reason):


print("Connection failed:", reason.getErrorMessage())
reactor.stop()

# Connect to server on localhost:8080


reactor.connectTCP("127.0.0.1", 8080, PingClientFactory())
reactor.run()

Output:

Traceroute simulation:

UDP server: (traceroute_server.py)

from twisted.internet import reactor


from twisted.internet.protocol import DatagramProtocol

class TracerouteServer(DatagramProtocol):
def datagramReceived(self, data, addr):
"""Handle incoming UDP packets and respond with hop information."""
print(f"Received packet from {addr[0]}:{addr[1]} - Data:
{data.decode().strip()}") # Debug message

try:
ttl = int(data.decode().strip())
except ValueError:
return

response = f"Hop {ttl}: {addr[0]}" if ttl > 1 else "Destination reached!"


self.transport.write(response.encode(), addr)

reactor.listenUDP(33434, TracerouteServer())
print("Traceroute server started on port 33434...") # Debug message
reactor.run()

UDP client: (traceroute_client.py)

from twisted.internet import reactor


from twisted.internet.protocol import DatagramProtocol

class TracerouteServer(DatagramProtocol):
def datagramReceived(self, data, addr):
"""Handle incoming UDP packets and respond with hop information."""
print(f"Received packet from {addr[0]}:{addr[1]} - Data:
{data.decode().strip()}") # Debug message

try:
ttl = int(data.decode().strip())
except ValueError:
return

response = f"Hop {ttl}: {addr[0]}" if ttl > 1 else "Destination reached!"


self.transport.write(response.encode(), addr)

reactor.listenUDP(33434, TracerouteServer())
print("Traceroute server started on port 33434...") # Debug message
reactor.run()

Output:
RESULT:
Thus the ping and traceroute commands are simulated and implemented in python
using twisted and the outputs are demonstrated.

You might also like