Skip to content

Session 9: Practice 2

Juan Gonzalez-Gomez edited this page Feb 26, 2020 · 40 revisions

Session 9: Practice 2

  • Time: 2h
  • Date: Wednesday, Feb-26th-2020
  • Goals:
    • Practicing with the creation of clients
    • More Practicing on creating classes and objects
    • Learn how to run servers and clients on the same machine
    • Get familiar with the typical socket and network errors that will occur

Contents

Introduction

The goal of this practice is to gain experience running servers and clients in the same computer. We will develop python programs (clients) that send messages and sequences to other servers

For making it easier to send the messages, we will develop the class Client located in the Client0 module (File Client0.py)

Method Parameters Return Description
__init__(ip, port) IP: String, Port: Integer None Object Constructor. Stores the IP and Port values inside the object
__str__() none String It returns the string: "Connection to SERVER at {ip}, PORT: {port}", where the {ip} and {port} are the server's ip and port respectivelly
ping() None None It prints the "OK" message on the console. It is just for testing the module
talk(msg) msg: String to send to the server The response message from the server It creates a new socket, connects to the server, sends the message, receive the response message from the server and closes the socket
debug_talk(msg) msg: String to send to the server The response message from the server It works the same than the talk method, but it prints on the console both the message sent to the server and the response received, in different colors

As always, this class will be created step by step in the exercises. The process is guided

Exercises

We will implement the Client Class and develop some example programs to test it against the server

Exercise 1: ping()

Let's start writing the Client Class. Create the file P2/Client0.py. Implement the __init__() and the ping() methods. Do not forget to mark the P2 folder as Sources Root

  • Filename: P2/Client0.py
  • Description: Class for sending messages easily to the server

Create the EX1.py File for testing the Client Class. It just creates a client object, calls the ping() method and prints the configured IP and PORT.

  • Filename: P2/EX1.py
  • Description: Preliminary test of the Client Class

The code should looks like this:

from Client0 import Client

PRACTICE = 2
EXERCISE = 1

print(f"-----| Practice {PRACTICE}, Exercise {EXERCISE} |------")

# -- Parameters of the server to talk to
IP = "192.168.1.45"
PORT = 8080

# -- Create a client object
c = Client(IP, PORT)

# -- Test the ping method
c.ping()

# -- Print the IP and PORTs
print(f"IP: {c.ip}, {c.port}")

When executed, this is what you should see (but with different values of the IP and PORT):

-----| Practice 2, Exercise 1 |------
OK!
IP: 192.168.1.45, 8080

Process finished with exit code 0

Exercise 2: __str__()

Implement the __str__() method. It should return the string:

"Connection to SERVER at {ip}, PORT: {port}"

Where the {ip} and {port} are the server's ip and port respectivelly

  • Filename: P2/EX2.py
  • Description: Testing the Printing of a Client object

After the client object is created, you should print it:

#...
c = Client(IP, PORT)
print(c)
#...

This is what you should see on the console:

-----| Practice 2, Exercise 2 |------
Connection to SERVER at 192.168.1.45, PORT: 8080

Process finished with exit code 0

No real connection has been established yet. It just prints the IP and port of the Client object

Exercise 3: talk()

Implement the talk() method in the Client Class. It should send a message to the server and return the response

This is the implementation. Make sure you understand the details

# -- Create the socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# establish the connection to the Server (IP, PORT)
s.connect((self.ip, self.port))

# Send data.
s.send(str.encode(msg))

# Receive data
response = s.recv(2048).decode("utf-8")

# Close the socket
s.close()

# Return the response
return response
  • Filename: P2/EX3.py
  • Description: Testing the talk() method. The client should send a message to the server and print the response from the server

After the client object is created, this example will send a message a print the response message:

...
# -- Send a message to the server
print("Sending a message to the server...")
response = c.talk("Testing!!!")
print(f"Response: {response}")
...

First you should launch the Teacher's server of Session-08 (server.py). Then execute the EX3.py program. This is what you see on the client console:

-----| Practice 2, Exercise 3 |------
Connection to SERVER at 192.168.1.45, PORT: 8080
Sending a message to the server...
Response: 

Hello from the teacher's server



Process finished with exit code 0

And if you have a look a the Server's console, this is what you should see:

Waiting for connections at 192.168.1.45, 8080 
CONNECTION: 1. From the IP: ('192.168.1.45', 40652)
Message from client: Testing!!!
Waiting for connections at 192.168.1.45, 8080 

Make sure you configure the server with YOUR IP and that the client uses the same IP and PORT than the server

Exercise 4: debug_talk()

Implement the debug_talk() method. It just call the talk() method and prints both messages on the console, in different colors:

  • Filename: P2/EX4.py
  • Description: Testing the debug_talk() method. The client should send two messages to the server
c.debug_talk("Message 1---")
c.debug_talk("Message 2: Testing !!!")

This is what you should see on the Client's console:

You should also see the messages on the Server's console:

Notice that the Teacher's server always response with the same message

Exercise 5: Sending a gene to the server

The Client Class is now ready. Let's use it for sending gene sequences to the server

  • Filename: P2/EX5.py
  • Description: Write a python program that sends the U5 gene from the file to the Teacher's server. It should initially send the message: "Sending the U5 Gene to the server..." and then the Gene itself

This is what you should see on the Client's console:

These are the messages received on the server:

Congrats! You've been able to transfer your first gene from one computer to another over the internet!

Exercise y

  • Open all the genes and send them to the server
  • Open a sequence from a file (ADA or whatever), calculate its complement, divide it into pieces of 20 bases and send to the server....

Exercise z

  • Launch two servers. Open a gene. Read 20 bases. If the initial base is 'A' or 'T' is sent to the server 1, if the base is 'C' or 'G' it is sent to the other server

END of the session

The session is finished. Make sure, during this week, that everything in this list is checked!

  • You have all the items of the session 6 checked!
  • Your working repo contains the P1 Folder with the following files:
    • Ex1.py
    • Ex2.py
    • Ex3.py
    • Ex4.py
    • Ex5.py
    • Ex6.py
    • Ex7.py
    • Ex8.py
    • Ex9.py
    • Ex10.py
    • Seq0.py
  • All the previous files have been pushed to your remote Github repo

Author

Credits

  • Alvaro del Castillo. He designed and created the original content of this subject. Thanks a lot :-)

License

Links