forked from abhishekpaturkar/SPPU-COMP-TE-SEM5-PRACTICALS
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba6d55e
commit dd34a08
Showing
62 changed files
with
2,205 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class LamportClock: | ||
def __init__(self): | ||
self.clock = 0 | ||
|
||
def get_time(self): | ||
return self.clock | ||
|
||
def tick(self): | ||
self.clock += 1 | ||
|
||
def main(): | ||
# Create two Lamport clocks | ||
clock1 = LamportClock() | ||
clock2 = LamportClock() | ||
|
||
# Simulate events and clock updates | ||
print(f'Initial Clock 1: {clock1.get_time()}') | ||
print(f'Initial Clock 2: {clock2.get_time()}') | ||
|
||
clock1.tick() | ||
print(f'Clock 1 after tick: {clock1.get_time()}') | ||
|
||
clock2.tick() | ||
print(f'Clock 2 after tick: {clock2.get_time()}') | ||
|
||
# Compare clock values | ||
if clock1.get_time() < clock2.get_time(): | ||
print('Clock 1 is behind Clock 2') | ||
elif clock1.get_time() > clock2.get_time(): | ||
print('Clock 2 is behind Clock 1') | ||
else: | ||
print('Clock 1 and Clock 2 are synchronized') | ||
|
||
if __name__ == "__main__": | ||
main() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import ntplib | ||
from time import ctime | ||
|
||
def sync_ntp_time(server): | ||
try: | ||
c = ntplib.NTPClient() | ||
response = c.request(server, version=3) | ||
local_time = ctime(response.tx_time) | ||
print(f'NTP Server: {server}') | ||
print(f'Local Time synchronized with NTP Server: {local_time}') | ||
except Exception as e: | ||
print(f'Error: {e}') | ||
|
||
def main(): | ||
ntp_server = 'pool.ntp.org' # Replace with a valid NTP server | ||
sync_ntp_time(ntp_server) | ||
|
||
if __name__ == "__main__": | ||
main() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import time | ||
import threading | ||
|
||
class Node: | ||
def __init__(self, id): | ||
self.id = id | ||
self.coordinator = None | ||
|
||
def initiate_ring_election(self, nodes): | ||
# Initiating the Ring election | ||
print(f"Node {self.id} initiates Ring election") | ||
max_id = max(node.id for node in nodes) # Compare node IDs | ||
if self.id == max_id: | ||
# If this node has the highest ID, it becomes the coordinator | ||
self.coordinator = self.id | ||
print(f"Node {self.id} becomes the coordinator.") | ||
return | ||
next_node = nodes[(nodes.index(self) + 1) % len(nodes)] | ||
next_node.start_ring_election(nodes) | ||
|
||
def start_ring_election(self, nodes): | ||
# Starting the Ring election process | ||
if self.coordinator is None: | ||
print(f"Node {self.id} passes the election message.") | ||
time.sleep(1) # Simulate message passing delay | ||
self.initiate_ring_election(nodes) | ||
|
||
def start_bully_election(self, nodes): | ||
# Starting the Bully election | ||
print(f"Node {self.id} starts the Bully election.") | ||
higher_nodes = [node for node in nodes if node.id > self.id] | ||
if not higher_nodes: | ||
# If no nodes have higher IDs, this node becomes the coordinator | ||
self.coordinator = self.id | ||
print(f"Node {self.id} becomes the coordinator.") | ||
return | ||
for higher_node in higher_nodes: | ||
higher_node.send_bully_election_message() | ||
|
||
def send_bully_election_message(self): | ||
# Sending a Bully election message to higher nodes | ||
print(f"Node {self.id} sends Bully election message.") | ||
time.sleep(1) # Simulate message passing delay | ||
print(f"Node {self.id} received no response, declares itself as coordinator.") | ||
self.coordinator = self.id | ||
|
||
def main(): | ||
nodes = [Node(1), Node(2), Node(3)] # Change the number of nodes as needed | ||
|
||
for node in nodes: | ||
# Each node participates in both Ring and Bully elections | ||
node.start_bully_election(nodes) | ||
node.start_ring_election(nodes) | ||
|
||
if __name__ == "__main__": | ||
main() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import xmlrpc.client | ||
|
||
def main(): | ||
# Create an XML-RPC client | ||
proxy = xmlrpc.client.ServerProxy("http://localhost:8000/RPC2") | ||
|
||
# Call remote methods | ||
a = int(input("Enter a : ")) | ||
b = int(input("Enter b : ")) | ||
result_add = proxy.add(a, b) | ||
result_subtract = proxy.subtract(a, b) | ||
|
||
print(f"Addition result: {result_add}") | ||
print(f"Subtraction result: {result_subtract}") | ||
|
||
if __name__ == "__main__": | ||
main() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from xmlrpc.server import * | ||
|
||
# Create a simple class with some methods | ||
class MathOperations: | ||
def add(self, x, y): | ||
return x + y | ||
|
||
def subtract(self, x, y): | ||
return x - y | ||
|
||
# Restrict to a particular path to avoid arbitrary code execution | ||
class RequestHandler(SimpleXMLRPCRequestHandler): | ||
rpc_paths = ('/RPC2',) | ||
|
||
def main(): | ||
server = SimpleXMLRPCServer(('localhost', 8000), requestHandler=RequestHandler) | ||
server.register_instance(MathOperations()) | ||
|
||
print("RPC Server is listening on port 8000...") | ||
server.serve_forever() | ||
|
||
if __name__ == "__main__": | ||
main() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import socket | ||
|
||
def main(): | ||
host = '127.0.0.1' # Use localhost | ||
port = 8080 | ||
|
||
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
client.connect((host, port)) | ||
|
||
message = input("Enter a message to send to the server: ") | ||
|
||
while True: | ||
client.send(message.encode()) | ||
data = client.recv(1024) | ||
print("Received from server: " + data.decode()) | ||
|
||
message = input("Enter another message (or press Enter to exit): ") | ||
if not message: | ||
break | ||
|
||
client.close() | ||
|
||
if __name__ == "__main__": | ||
main() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import socket | ||
import threading | ||
|
||
# Function to handle a client connection | ||
def handle_client(client_socket): | ||
while True: | ||
data = client_socket.recv(1024) | ||
if not data: | ||
break | ||
client_socket.send(data) | ||
client_socket.close() | ||
|
||
def main(): | ||
host = '127.0.0.1' # Use localhost | ||
port = 8080 | ||
|
||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
server.bind((host, port)) | ||
server.listen(5) | ||
|
||
print(f"[*] Listening on {host}:{port}") | ||
|
||
while True: | ||
client_socket, addr = server.accept() | ||
print(f"[*] Accepted connection from: {addr[0]}:{addr[1]}") | ||
|
||
# Create a new thread to handle the client | ||
client_handler = threading.Thread(target=handle_client, args=(client_socket,)) | ||
client_handler.start() | ||
|
||
if __name__ == "__main__": | ||
main() |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.