Python Server/Client Sockets help

Discussion in 'Programming' started by Tristan Houghton, Dec 6, 2022.

Tags:
  1. Tristan Houghton

    Tristan Houghton New Member

    Joined:
    Dec 6, 2022
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    I am trying to learn server/client sockets in python. I can establish the server and client and make the connection. I would like the server to ask the client to input a command. I would like the command to be TIME and I would like the response to display the current time. How do I do that?

    Here is my server code:
    Code:
    #shell_server.py for python3
    import socket
    import subprocess
    
    s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    #s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(("0.0.0.0",444))
    s.listen(1)
    
    while True:
       connexion, client_address = s.accept()
       print ("connexion from :" +str(client_address))
       while True:
           connexion.send(bytes("Entrer cmd : ", "utf-8"))
           cmd = connexion.recv(1024).decode()
          # p = subprocess.Popen(cmd.split(" "),shell=True
           p = subprocess.Popen(cmd,shell=True
               ,stdout=subprocess.PIPE,stderr = subprocess.PIPE)
           out , err = p.communicate()
           connexion.send(out)
    
    And here is my client code:
    Code:
    #shell_client.py for python3
    import socket
    
    s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect(("127.0.0.1",444))
    
    while True:
       data = s.recv(4096)
       print(data.decode())
       cmd = input()
       s.send(cmd.encode('utf-8'))
    
    So how do I input TIME and receive the current time?
     
  2. nickmistman

    nickmistman New Member

    Joined:
    Oct 20, 2021
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Did you solve this task?
     
  3. Adnan

    Adnan New Member

    Joined:
    Nov 7, 2022
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    i think you have to use python datetime module in server file, and in server file you have to check the client sent text if its "Time" then simply send
    datetime.now()
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice