You are on page 1of 10

Python Programming

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()))

msg = input("Enter the message for server: ")

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

# make up some data in the interval [0, 1]


y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

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

# make up some data in the interval [0, 1]


y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

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

# make up some data in the interval [0, 1]


y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

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

# Fixing random state for reproducibility


np.random.seed(19680801)

mu, sigma = 100, 15


x = mu + sigma * np.random.randn(10000)

# the histogram of the data


n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)

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

# evenly sampled time at 200ms intervals


t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles


plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
Output: -

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)]

print("\nEncryped message: ", encrypt)


decrypt_key = int(input("Enter the decrypting key: "))

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()

''' Label '''


label= tkinter.Label(top, text="Hello", font='algerian -48 bold')
label.pack()

''' Button '''


button=tkinter.Button(top, text="Click Here", command=top.quit, bg='#0e0e0e',
fg='white')
button.pack(fill=tkinter.X, expand=1)

top.mainloop()

top1 = tkinter.Tk()
label1= tkinter.Label(top1, text="Hi there ", font='Comic_sans_ms -42 bold')
label1.pack()

top1.mainloop()

Output: -

22

You might also like