Escolar Documentos
Profissional Documentos
Cultura Documentos
Practical 7
AIM: - Develop chat room application using multithreading.
#server.py
import socket
from threading import Thread
host_name = '127.0.0.1'
port = 6996
class ClientThread(Thread):
def __init__(self, ip, port):
Thread.__init__(self)
self.ip = ip
self.port = port
print("[+] New server socket thread started for " + ip + ":" + str(port))
def run(self):
while True:
data = conn.recv(1024)
print("Server received data:", data.decode())
MESSAGE = input("Multithreaded Python server : Enter Response from Server/Enter
exit:")
if MESSAGE == 'exit':
break
conn.send(MESSAGE.encode())
soc = socket.socket()
soc.bind((host_name,port))
threads = []
while True:
soc.listen(4)
print("Multithreaded Python server : Waiting for connections from TCP clients...")
(conn, (ip, port)) = soc.accept()
newthread = ClientThread(ip, port)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
13
Python Programming
#client.py
import socket
host_name = '127.0.0.1'
port = 6996
csoc = socket.socket()
csoc.connect((host_name,port))
msg = input("Enter the message for server: ")
while ((msg!='q')):
if msg.lower()=='quit':
break
csoc.send(msg.encode())
temp = csoc.recv(1024)
print("Msg from server: ", str(temp.decode()))
csoc.close()
Output: -
#server_side
14
Python Programming
#client1
#client2
15
Python Programming
Practical-8
AIM: - Learn to plot different types of graphs using PyPlot.
#Linear Graph
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)
plt.show()
Output: -
16
Python Programming
#Log
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)
plt.show()
Output: -
#Symmetric Log
import numpy as np
import matplotlib.pyplot as plt
17
Python Programming
plt.subplot(221)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('Symmetric Log')
plt.grid(True)
plt.show()
Output: -
#Histogram
import numpy as np
import matplotlib.pyplot as plt
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
18
Python Programming
plt.show()
Output: -
#Different shapes
import numpy as np
import matplotlib.pyplot as plt
19
Python Programming
Practical-9
AIM: - Implement classical ciphers using python.
#Ceasar Cipher
pt = input("Enter the message: ")
key = (int)(input('Enter the key: '))
characters = "abcdefghijklmnopqrstuvwxyz "
encrypt=''
decrypt=''
pt = pt.lower()
for c in pt:
if c in characters:
encrypt += characters[(characters.index(c)+key) % len(characters)]
if decrypt_key == key:
for c in encrypt:
if c in characters:
decrypt += characters[(characters.index(c)-key) % len(characters)]
print("\nDecryped message: ", decrypt)
else:
print("Enter key is not the decrypting key")
Output: -
20
Python Programming
Practical 10
AIM: - Draw graphics using Turtle.
import turtle
spiral = turtle.Turtle()
for i in range(20):
spiral.forward(i * 10)
spiral.right(144)
turtle.done()
Output: -
21
Python Programming
Practical 11
AIM: - Develop programs to learn GUI programming using Tkinter.
import tkinter
top = tkinter.Tk()
top.mainloop()
top1 = tkinter.Tk()
label1= tkinter.Label(top1, text="Hi there ", font='Comic_sans_ms -42 bold')
label1.pack()
top1.mainloop()
Output: -
22