Python Experiment No: 10 Implementation of TCP Client And TCP Server.
Aim:- Implementation of TCP Client And TCP Server.
Theory:-
Networking allows computer programs to communicate with each other, even if they are running on different machines. For programs such as web browsers, this is the essence of what they do, whereas for others networking adds additional dimensions to their functionality, for example, remote operation or logging, or the ability to retrieve or supply data to other machines. Most networking programs work on either a peer-to-peer basis (the same program runs on different machines), or more commonly, a client/server basis (client programs send requests to a server).
Theory:-
Networking allows computer programs to communicate with each other, even if they are running on different machines. For programs such as web browsers, this is the essence of what they do, whereas for others networking adds additional dimensions to their functionality, for example, remote operation or logging, or the ability to retrieve or supply data to other machines. Most networking programs work on either a peer-to-peer basis (the same program runs on different machines), or more commonly, a client/server basis (client programs send requests to a server).
Creating a TCP Client:-
def main():
if len(sys.argv) > 1:
Address[0] = sys.argv[1]
call = dict(c=get_car_details, m=change_mileage, o=change_owner,
n=new_registration, s=stop_server, q=quit)
menu = ("(C)ar Edit (M)ileage Edit (O)wner (N)ew car "
"(S)top server (Q)uit")
valid = frozenset("cmonsq")
previous_license = None
while True:
action = Console.get_menu_choice(menu, valid, "c", True)
previous_license = call[action](previous_license)
if len(sys.argv) > 1:
Address[0] = sys.argv[1]
call = dict(c=get_car_details, m=change_mileage, o=change_owner,
n=new_registration, s=stop_server, q=quit)
menu = ("(C)ar Edit (M)ileage Edit (O)wner (N)ew car "
"(S)top server (Q)uit")
valid = frozenset("cmonsq")
previous_license = None
while True:
action = Console.get_menu_choice(menu, valid, "c", True)
previous_license = call[action](previous_license)
The Address list is a global that holds the IP address and port number as a two-item list, ["localhost", 9653], with the IP address overridden if specified on the command line. The call dictionary maps menu options to functions.
Since the loop is infinite the program must be terminated by one of the functions; we will see this further on.
Since the loop is infinite the program must be terminated by one of the functions; we will see this further on.
def get_car_details(previous_license):
license, car = retrieve_car_details(previous_license)
if car is not None:
print("License: {0}\nSeats: {seats}\nMileage: {mileage}\n"
"Owner: {owner}".format(license, **car._asdict()))
return license
license, car = retrieve_car_details(previous_license)
if car is not None:
print("License: {0}\nSeats: {seats}\nMileage: {mileage}\n"
"Owner: {owner}".format(license, **car._asdict()))
return license
This function is used to get information about a particular car. Since most of the functions need to request a license from the user and often need some car-related data to work.
Creating a TCP Server:-
Since the code for creating servers often follows the same design, rather than having to use the low-level socket module, we can use the high-level socketserver module which takes care of all the housekeeping for us. All we have to do is provide a request handler class with a handle() method which is used to read
requests and write replies. The socketserver module handles the communications for us, servicing each connection request, either serially or by passing each request to its own separate thread or process—and it does all of this transparently so that we are insulated from the low-level details.
Since the code for creating servers often follows the same design, rather than having to use the low-level socket module, we can use the high-level socketserver module which takes care of all the housekeeping for us. All we have to do is provide a request handler class with a handle() method which is used to read
requests and write replies. The socketserver module handles the communications for us, servicing each connection request, either serially or by passing each request to its own separate thread or process—and it does all of this transparently so that we are insulated from the low-level details.
def main():
filename = os.path.join(os.path.dirname(__file__),
"car_registrations.dat")
cars = load(filename)
print("Loaded {0} car registrations".format(len(cars)))
RequestHandler.Cars = cars
server = None
try:
server = CarRegistrationServer(("", 9653), RequestHandler)
server.serve_forever()
except Exception as err:
print("ERROR", err)
finally:
if server is not None:
server.shutdown()
save(filename, cars)
print("Saved {0} car registrations".format(len(cars)))
filename = os.path.join(os.path.dirname(__file__),
"car_registrations.dat")
cars = load(filename)
print("Loaded {0} car registrations".format(len(cars)))
RequestHandler.Cars = cars
server = None
try:
server = CarRegistrationServer(("", 9653), RequestHandler)
server.serve_forever()
except Exception as err:
print("ERROR", err)
finally:
if server is not None:
server.shutdown()
save(filename, cars)
print("Saved {0} car registrations".format(len(cars)))
We have stored the car registration data in the same directory as the program. The cars object is set to a dictionary whose keys are license strings and whose values are Car objects. Normally servers do not print anything since they are typically started and stopped automatically and run in the background, so usually they report on their status by writing logs (e.g., using the logging module). Here we have chosen to print a message at start-up and shutdown to make testing and experimenting easier.
Q1) Explain TCP Client in detail.
Q2) Explain TCP Server in detail.
Q3) Explain communication of TCP Client and TCP Server
Q3) Explain communication of TCP Client and TCP Server
Comments
Post a Comment