Question
For this week, you will be creating two small Python3 programs that will communicate over a TCP/IP socket. This exercise will help illustrate one way
For this week, you will be creating two small Python3 programs that will communicate over a TCP/IP socket. This exercise will help illustrate one way for two different processes to communicate with each other.
The first python program should be called upper_server.py. This program will act as a server, binding to a port on your computer and waiting for socket clients to connect. When a client connects, the upper_server.py program will wait until the client sends a message. Each message will be ASCII based and delimited by a newline. For each message received, the upper_server.py program will convert the contents into the designated case and send the response back to the client, with each message terminated again by a newline. The upper_server.py program should accept a --port command line parameter, indicating what port the program should listen on for clients. By default, upper_server.py should convert any incoming text to lower case before echoing back to the client. If the program receives the text 'UPPER' (case-insensitive), then it should upper case the contents of any echo'd content. The incoming string 'lower' (case-insensitive) should revert back to subsequent output to lower case. Make sure to echo back the commands 'UPPER' and 'LOWER' back to the client, with the appropriate case changes.
The second python program should be called upper_client.py. This program will connect to localhost (which is your computer) and to a specified port. Once connected to the server, it will request the user for input. Whatever input the user enters (terminated by hitting the ENTER key) will be sent to the upper_server.py program over the socket. Each response from the upper_server.py program will be printed out to the screen, then the upper_client.py program will accept additional user input. The upper_client.py program continue performing the cycle of request input, send to server, print response until the program is terminated. The upper_client.py program should also accept a --port command line parameter, indicating what port it should try to connect to.
Both programs should use the socket and argparse modules that come included with Python.
Each program should be started in separate terminal windows, with the upper_server.py program started first. Both programs should run until the user terminates the program (i.e. hitting CTRL-C on a Mac).
Below are sample outputs from each program.
Output for both the questions:
upper_server.py example python3 upper_server.py --port 20000 Starting up on localhost:20000 Waiting for connection... Accepted new client received: 'Hello World' sending: 'hello world' received: 'upper' sending: 'UPPER' received: 'Hello World' sending: 'HELLO WORLD' received: 'lower' sending: 'lower' received: 'Hello World' sending: 'hello world' Client disconnected Waiting for connection...
upper_client.py example python3 upper_client.py --port 20000 Connecting to localhost:20000 ... Connected! Enter input: Hello World received: 'hello world' Enter input: upper received: 'UPPER' Enter input: Hello World received: 'HELLO WORLD' Enter input: lower received: 'lower' Enter input: Hello World received: 'hello world'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started