Hi guys hope you are all keeping cool.
i am having an issue with my python program that I was writing. I am trying to make small server & client program for high school students to motivate them to learn coding. but i am running into difficulties.
i was trying to get the user input from the command line (ubuntu to be precise) so like when a student enters the command to run the program with arguments (port number, buffer length & IP address) the program runs the client part of it. client then takes the length of buffer, takes the port number and ip from command line and assigns to the variables. it then generates a random number between 1 and 100 and sends to the port entered. Then when they enter the command to run the server side code with argument (port number only), the server listen to the port and receives the data and sends it back to the client.
the buffer length is to specify how much data the client will transmit to server.
the commands that student put are as follows.
./clientServer.py –t –p 5001 –l 1000 –ip 127.0.0.1
where -t to run the client part of code
-p to indicate port number
-l for buffer length
-IP for loop back address. (in real environment the same code will run on 2 different machines, one will run the client part of code and one will run the server part of code)
./clientServer.py –r –p 5001
where -r to run the server part of code
-p for the port number
but i am having issues in parsing data and running the file.
so if you guys can help me i would really appreciate that and will be really thankful to you guys.
here is the code
The program is not Running and also not assigning the values to the variables.
can anyone help me please.
i am having an issue with my python program that I was writing. I am trying to make small server & client program for high school students to motivate them to learn coding. but i am running into difficulties.
i was trying to get the user input from the command line (ubuntu to be precise) so like when a student enters the command to run the program with arguments (port number, buffer length & IP address) the program runs the client part of it. client then takes the length of buffer, takes the port number and ip from command line and assigns to the variables. it then generates a random number between 1 and 100 and sends to the port entered. Then when they enter the command to run the server side code with argument (port number only), the server listen to the port and receives the data and sends it back to the client.
the buffer length is to specify how much data the client will transmit to server.
the commands that student put are as follows.
./clientServer.py –t –p 5001 –l 1000 –ip 127.0.0.1
where -t to run the client part of code
-p to indicate port number
-l for buffer length
-IP for loop back address. (in real environment the same code will run on 2 different machines, one will run the client part of code and one will run the server part of code)
./clientServer.py –r –p 5001
where -r to run the server part of code
-p for the port number
but i am having issues in parsing data and running the file.
so if you guys can help me i would really appreciate that and will be really thankful to you guys.
here is the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
import socket import time import argparse from random import randint def main(): clientSock = socket.socket(socket.AF_INET, socket. SOCK_STREAM) #here is the argv parses parser = argparse.ArgumentParser(description = 'sending data to server and back to client' ) parser.add_argument( '-t' , '--transmit' , action = 'store_true' , help = 'transmission' ) parser.add_argument( '-r' , '--receive' , action = 'store_true' , help = 'receiving' ) parser.add_argument( '-ip' , '--serveripAddr' , default = ( '127.0.0.1' ), required = True , help = 'this is ip address' ) parser.add_argument( '-p' , '--serverport' , type = int , default = 5001 , required = True , help = 'this is port number' ) parser.add_argument( '-l' , '--bufferSize' , type = int , default = 8192 , required = True , help = 'this is buffer size' ) args = parser.parse_args() serverAddr = args.ip port = args.p buffersize = args.l if args.t: clientSock.connect(port, serverAddr, buffersize) number = randint( 1 , 100 ) dataOutStr = bin (number).replace( "0b" , "") # converts the numbers into binary before sending dataOutBytes = dataOutStr.encode() start = time.time() #start timer clientSock.sendall(dataOutBytes) # sending data numBytes = len (dataOutBytes) #===================================================================================================== #loop until all data receivedd dataInBytes = b'' while len (dataInBytes) < numBytes: dataInBytes + = clientSock.recv(numBytes - len (dataInBytes)) if not dataInBytes: break dataInStr = dataInBytes.decode() remoteAddr = clientSock.getpeername() print ( "received" , dataInStr, "back from server" , remoteAddr) print ( "it took " , time.time() - start) clientSock.close() elif args.r: serverSock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) serverSock.bind(('', 5001 )) serverSock.listen() while True : connectSock, clientAddr = serverSock.accept() print ( "Connection from" , clientAddr) while True : # keep receiving until client close message = connectSock.recv() if not message: break connectSock.sendall(message) connectSock.close() else : print ( "something went wrong" ) if __name__ = = '__main__' : main() |
The program is not Running and also not assigning the values to the variables.
can anyone help me please.