Chapter 10 Exercise Set 0: Chapter Review

Sockets For Two-Way Chat

The goal of this exercise is to Modify the example socket code to create a single program that can both send and receive messages. To achieve this, you will need to make several modifications.

  1. First, modify the socket_client program so that it allows the user to type the message at the command line. Your new program should have a while True loop, just like the server program. Inside the loop, it should use input() to receive messages from the command prompt.

  2. Next, combine the socket_client and socket_server programs into a single program that sends and receives messages. Your goal will be to run the same program twice on your computer so that you can chat with yourself. Here are a few things to think about if you want to make this work:

    • This program will work as both a client and a server. Each server needs its own port, so the program will need to know its own port number and the port number of the program it’s communicating with. Use input() to ask for both of these port numbers when the program starts. You can use 5005 and 5006 for the two port numbers.

    • While your program is waiting for you to type a message, it cannot also be listening to receive a message. To handle this constraint, we’ll have the two programs take turns - when one program is sending the other is listening. Come up with a strategy to keep track of which program is sending and which is listening.

    • Note: real chat applications do not require chatters to take turns, they can both send and listen at the same time using Treads, which are not covered in this text.

API Requests with Python

TODO: Add an activity here that uses requests to make a request to an API, and then parse the returned data.