Guest User

Server

a guest
May 9th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import socket
  2. import subprocess
  3. import os
  4.  
  5.  
  6.  
  7. def check_ping(hostname):
  8.     response = os.system("ping " + hostname)
  9.     # and then check the response...
  10.     if response == 0:
  11.         pingstatus = "Network Active"
  12.     else:
  13.         pingstatus = "Network Error"
  14.    
  15.     return(pingstatus)
  16.  
  17. def run_path(path):
  18.     response = os.system(path)
  19.     # and then check the response..
  20.     return('ok')
  21.  
  22. def Main():
  23.     host = "localhost"
  24.     port = 5000
  25.      
  26.     mySocket = socket.socket()
  27.     mySocket.bind((host,port))
  28.      
  29.     mySocket.listen(1)
  30.     conn, addr = mySocket.accept()
  31.     print ("Connection from: " + str(addr))
  32.     while True:
  33.             data = conn.recv(1024).decode()
  34.             if not data:
  35.                     break
  36.             #print ("from connected  user: " + str(data))
  37.              
  38.             #data = str(data)
  39.             #hostname = data.replace('ping ', '')
  40.            
  41.             print ("sending: " + str(data))
  42.             #print(hostname)
  43.             conn.send(data.encode())
  44.             if 'ping' in str(data):
  45.                 hostname = data.replace('ping ', '')
  46.                 pingstatus = check_ping(hostname)
  47.                 print(pingstatus)
  48.             elif 'open' in str(data):
  49.                 path = str(data).replace('open ', '')
  50.                
  51.                 run_path(path)
  52.                
  53.                
  54.                
  55.            
  56.              
  57.     conn.close()
  58.      
  59. if __name__ == '__main__':
  60.     Main()
Add Comment
Please, Sign In to add comment