0

i got a challenge to create:

messaging system comprised of 2 components - client and server.

3 types of messaging:

1) respond with current server time

2) respond with number of calls made to the server so far(*since it started running)

3) multiplication of two numbers.

i have tried to do that with python, so i saw this video tutorial :

https://www.youtube.com/watch?v=WrtebUkUssc

and read this guide:

http://www.bogotobogo.com/python/python_network_programming_server_client.php

but i have a big lack understanding, how the server knows what the client asking for?

and how the server can respond for each query, what can i see its only the bytes receive..

i hope that someone can help me i have only 24 hours to answer that (it's a challenge for work)

  • note: im a new computer science student.

thank you all!!

Eliran Suisa
  • 49
  • 1
  • 8

2 Answers2

0

If I would be in your situation I would not try to go deep and learn all the stuff that is happening behind the scenes.

First I would check what I am supposed to achieve:

1) respond with current server time:

  • have server up and running
  • be able to receive request via http GET method on some route (example /time)
  • be able to send response
  • pick reponse format (json is a good choice)
  • know if your data is valid in that format (for json you can use https://jsonlint.com/)
  • send response containing server time

2) respond with number of calls made to the server so far(*since it started running):

it is not clear what you are asked to do. Maybe server should be able to respond more than once?

3) multiplication of two numbers:

  • pick http method that you will support. If you pick http GET client will have to send those numbers in url like /multiply/2/3/. If you pick http POST you will have to send data (also called payload) that is not visible in url like /multiply
  • be able to send response
  • pick reponse format (json is a good choice)
  • know if your data is valid in that format (for json you can use https://jsonlint.com/)
  • send response containing answer (example response data could look like {"answer": 6})

Pick server to code first. Learn a bit about http by looking what is request, response, methods GET, PUT, POST and what is http status code. When you will be coding you have to know some basic http status codes like 200, 404, 400. Use easy web framework like flask so that you should not have to deal with udp, tcp, sockets and other irrelevant stuff. Learn how to make http request to your server. curl is a good option but if you want something else make sure that your client will show you response status code, headers and data. Last but not least don't get stuck as getting stuck will burn your time very quick.

Žilvinas Rudžionis
  • 1,954
  • 20
  • 28
0

The article that you have linked to provides enough information for you to create a client and server to do what you are asking.

Note: this answer focuses on the raw socket connections, for HTTP: see the answer by @ŽilvinasRudžionis

how [does] the server knows what the client asking for?

It doesn't. Atleast not intrinsically. Which is why you need to write the code to make it understand what the client it asking. The best part of the article to understand this is the Echo Server section.

To begin understanding you will want to follow this example.

# echo_server.py
import socket

host = ''                                  # Symbolic name meaning all available interfaces
port = 12345                               # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET,          # See [1]
                  socket.SOCK_STREAM) 
s.bind((host, port))                       # Server claims the port
s.listen(1)                                # See [2]
conn, addr = s.accept()                    # This is code halting, everything
                                           # will hold here and wait for a connection
print('Connected by', addr)

while True:
    data = conn.recv(1024)                # Receive 1024 bytes
    if not data: break                    # If data is Falsey (i.e. no data) break
    conn.sendall(data)                    # Send all data back to the connection
conn.close()

[1] [2]


# echo_client.py
import socket

host = socket.gethostname()               # Server is running on the same host
port = 12345                              # The same port as used by the server
s = socket.socket(socket.AF_INET,         # See server code ([1])
                  socket.SOCK_STREAM)
s.connect((host, port))                   # Initiate the connection
s.sendall(b'Hello, world')                # Send some bytes to the server
data = s.recv(1024)                       # Receive some bytes back
s.close()                                 # Close the connection
print('Received', repr(data))             # Print the bytes received

This gives you a basic framework to see what is going on. From here, you can add your own code to inspect the data and connections.

The most important part is the bit where you accept connections on the server. Here, you can check the data received and respond appropriately. For time, the first example in the article shows how to do this.

#--snip--
import time

# Receive all data
while True:
    data = conn.recv(1024)
    if not data: break

# Now we can check the data and prepare the response
if data == "TIME":
    return_data = (time.ctime(time.time()) + "\r\n").encode('ascii')
elif data == "REQUEST COUNT":
    # Code to get request count
elif "MULTI" in data:
    # Example: MULTI 2 3
    data = data.split(" ")  # Becomes ["MULTI", "2", "3"]
    # Code to multiply the numbers

# Send the result back to the client
conn.sendall(return_data)

I have created a github gist for the time example that you can download and play with. It should work with both Python 2 and 3.

<script src="https://gist.github.com/alxwrd/b9133476e2f11263e594d8c29256859a.js"></script>
alxwrd
  • 2,320
  • 16
  • 28