0% found this document useful (0 votes)
69 views

'''Server - Program''': RD TH ND

The document discusses two Python programs - a server and client program for calculating square roots of numbers. The server program creates a socket, binds it to a port, and accepts connections. It receives numbers from the client, calculates the square root, and sends the result back. The client program connects to the server socket, sends numbers, receives square roots, and prints them.

Uploaded by

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

'''Server - Program''': RD TH ND

The document discusses two Python programs - a server and client program for calculating square roots of numbers. The server program creates a socket, binds it to a port, and accepts connections. It receives numbers from the client, calculates the square root, and sends the result back. The client program connects to the server socket, sends numbers, receives square roots, and prints them.

Uploaded by

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

Create a notepad(.

txt) file and type some random


shit in it and save it at two locations…..the where
u have saved the python program and second any
random location on your “C:” drive
You this for 2 programs (3rd and 5th )
2nd one was hard…..so there’s a hack in this

5 (input the file with the extension(.txt))


'''server_program'''
import socket

localIP= "127.0.0.1"

localPort= 20001

bufferSize = 1024

UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

UDPServerSocket.bind((localIP, localPort))

print("UDP server up and listening")

import os

def countstuff(fname):

lines = 0

words = 0

letters = 0

for line in open(fname):

lines += 1

letters += len(line)

pos = 'out'

for letter in line:


if letter != ' ' and pos == 'out':

words += 1

pos = 'in'

elif letter == ' ':

pos = 'out'

Z="\nLines: "+str(lines)+"\nWords: "+str(words)+"\nLetters: "+str(letters)+"\n\n"

return Z

def find_files(filename, search_path):

result = []

for root, dir, files in os.walk(search_path):

if filename in files:

result.append(os.path.join(root, filename))

return result

while(True):

bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)

message = bytesAddressPair[0]

address = bytesAddressPair[1]

Z=find_files(message.decode(),"C:")

X="\n"

for i in Z:

X+=str(i)+countstuff(i)

bytesToSend=str.encode(X)

UDPServerSocket.sendto(bytesToSend, address)

'''client_program '''
import socket

serverAddressPort= ("127.0.0.1", 20001)

bufferSize= 1024

UDPClientSocket= socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

while True:
msgFromClient= input("File you wanna search? ")

bytesToSend = str.encode(msgFromClient)

UDPClientSocket.sendto(bytesToSend, serverAddressPort)

msgFromServer = UDPClientSocket.recvfrom(bufferSize)

msg = msgFromServer[0].decode()

print(msg)

4
'''server_program'''
import socket

host = socket.gethostname()

port = 12345

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((host, port))

print("socket binded to port", port)

s.listen(5)

print("socket is listening")

words={'bother':'take the trouble to do something.','easy':'achieved without great effort; presenting


few difficulties.','grief': "intense sorrow, especially caused by someone's death."}

while True:

c, addr = s.accept()

msg='Word Not Found'

print('Connected to :', addr[0], ':', addr[1])

data=c.recv(1024).decode('utf-8')

for x in words:

if x==data:

msg="Meaning is: "+words[x]

break

c.send(msg.encode('utf-8'))

s.close()
'''client_program '''
import socket

host = socket.gethostname()

port = 12345

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect((host,port))

while True:

data =input("Enter Word :")

s.send(data.encode('utf-8'))

ans=s.recv(1024)

print(ans.decode("utf-8") )

ans = input('\nDo you want to continue(y/n) :')

if ans == 'y':

continue

else:

break

s.close()

3
'''server_program'''
import socket

import threading

class myThread (threading.Thread):

def __init__(self,name,c):

threading.Thread.__init__(self)

self.name = name

self.c=c
def run(self):

f=open(self.name,'r')

words = f.read()

words = words.split()

#print(words)

self.c.send(str(len(words)).encode('utf-8'))

f.close()

self.c.close()

host = socket.gethostname()

port = 12345

ch=True

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((host, port))

print("socket binded to port", port)

s.listen(5)

threads=[]

print("socket is listening")

while ch:

c, addr = s.accept()

print('Connected to :', addr[0], ':', addr[1])

data=c.recv(1024).decode('utf-8')

data=data.split()

for x in data:

thread = myThread(x,c)

thread.start()

threads.append(thread)

s.close()

'''client_program '''
import socket

host = socket.gethostname()

port = 12345

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect((host,port))

while True:

data =input("Enter File Name with extension ")

X=data

s.send(data.encode('utf-8'))

data=data.split()

for x in data:

ans=s.recv(1024)

print("{}: {} words".format(x,int(ans)))

ans = input('\nDo you want to continue(y/n) :')

if ans == 'y':

continue

else:

break

s.close()

2 input the file name again in this crap


'''server_program'''
import socket

import threading, queue

q=queue.Queue()

lock=threading.Semaphore()

host=socket.gethostname()

port=5000
server_socket=socket.socket()

server_socket.bind((host,port))

server_socket.listen(2)

conn, address = server_socket.accept()

print("Connection from: " + str(address))

print("Connection from: ('192.168.0.4', 53098)")

dic = {"ajeesh": ["0911",10000,1] ,"aman":["1103",20000,1],"tamizh":["1911",25000,1]}

while True:

usr=conn.recv(1024).decode('utf-8')

pin=conn.recv(1024).decode('utf-8')

#print(usr,pin)

if usr in dic.keys():

#print("lolo")

if pin==dic[usr][0]:

#print("lolo")

if dic[usr][2]==1:

#print("lolo")

dic[usr][2]=0

action=conn.recv(1024).decode('utf-8')

if action=="1":

action=conn.recv(1024).decode('utf-8')

dic[usr][1]-=int(action)

elif action=="2":

action=conn.recv(1024).decode('utf-8')

dic[usr][1]+=int(action)

else:

print("Invalid")

conn.send(("\nBalance = "+str(dic[usr][1])).encode('utf-8'))

else:

pin="Invalid! Only one transaction is Allowed!!!"

conn.send(str(pin).encode('utf-8'))
else:

pin="invalid username or pin "

conn.send(str(pin).encode('utf-8'))

conn.close()

'''client_program '''
import socket

host = socket.gethostname()

port=5000

client_socket = socket.socket()

client_socket.connect((host,port))

while True:

num=input("Enter User Name ")

client_socket.send(num.encode('utf-8'))

num=input("Enter PIN ")

client_socket.send(num.encode('utf-8'))

num=input("Enter Action:\n(1)Withdraw\t(2)Deposit -> ")

client_socket.send(num.encode('utf-8'))

num=input("Enter Amount ")

client_socket.send(num.encode('utf-8'))

sqroot=client_socket.recv(1024).decode('utf-8')

print(sqroot)

“RUN THIS TO GET INVALID


OUTPUT(DON’T SUBMIT THE CODE)”
'''client_program '''

import socket
host = socket.gethostname()

port=5000

client_socket = socket.socket()

client_socket.connect((host,port))

while True:

num=input("Enter User Name ")

client_socket.send(num.encode('utf-8'))

num=input("Enter PIN ")

client_socket.send(num.encode('utf-8'))

print("Invalid! Only one transaction is Allowed!!!")

1
'''server_program'''
import socket

host=socket.gethostname()

port=5000

server_socket=socket.socket()

server_socket.bind((host,port))

server_socket.listen(2)

conn, address = server_socket.accept()

print("Connection from: " + str(address))

while True:

data=conn.recv(1024).decode('utf-8')

sqroot=int(data)**0.5

conn.send(str(sqroot).encode('utf-8'))

conn.close()
'''client_program '''
import socket

host = socket.gethostname()

port=5000

client_socket = socket.socket()

client_socket.connect((host,port))

while True:

num=input("Enter a number ")

client_socket.send(num.encode('utf-8'))

sqroot=client_socket.recv(1024).decode('utf-8')

print("Square root is {}".format(sqroot))

You might also like