Commit d1127554 authored by Bonnegent's avatar Bonnegent
Browse files

client / serveur utilisant les sockets

parent c041cd35
#!/usr/bin/env python3
import socket
def main(host="localhost", port=7777):
mySocket = socket.socket()
try:
mySocket.connect((host,port))
except ConnectionRefusedError:
print('connection error')
return False
# peu importe le message
message = "get /"
mySocket.send(message.encode())
data = mySocket.recv(1024).decode()
print ('received from %s:%d: [%s]' % (host, port, data))
mySocket.close()
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
main(host=argv[1])
else:
main()
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import platform
import os
import socket
PORT = 7777
VERSION = "17.12.18"
class MyServer(BaseHTTPRequestHandler):
# GET is for clients geting the predi
def do_GET(self):
"""Va retourner les informations suivantes sur une même ligne,
en utilisant un ';' comme séparateur.
On a mis de côté les infos suivantes car non multiplateforme par défaut
- nombre d'utilisateur connecté
- uptime
Exemple:
os;os_version;charge
"""
self.send_response(200)
# partie os: le nom du système d'exploitation
VERSION = "18.01.08"
def main(host="localhost", port=7777):
"""Va retourner les informations suivantes sur une même ligne,
en utilisant un ';' comme séparateur.
On a mis de côté les infos suivantes car non multiplateforme par défaut
- nombre d'utilisateur connecté
- uptime
Exemple:
os;os_version;charge
"""
mySocket = socket.socket()
mySocket.bind((host,port))
print("starting OSInfo [%s] ..." % VERSION)
mySocket.listen(1)
while True:
conn, addr = mySocket.accept()
print("connection from: " + str(addr))
data = conn.recv(1024).decode()
#if not data:
# break
print("received from client: " + str(data))
# make data
os_name = "inconnue"
os_version = platform.version()
os_load = "inconnue"
......@@ -35,50 +42,50 @@ class MyServer(BaseHTTPRequestHandler):
# distrib = ('Fedora', '26', 'Twenty Six')
distrib = platform.dist()
os_name = "Linux %s" % " ".join(distrib[:2])
os_version = self.get_os_version()
os_load = self.get_load()
os_version = get_os_version()
os_load = get_load()
elif system == 'Windows':
# Windows 10
os_name = "%s %s" % (system, platform.release())
message = "%s;%s;%s" % (os_name, os_version, os_load)
self.wfile.write(bytes(message, "utf-8"))
def get_os_version(self):
"Cas des postes pédago"
file_version = "/etc/version"
try:
fd = open(file_version, 'r')
tmp = fd.readline().split('\n')[0]
except Exception:
version = 'inconnue'
else:
version = tmp
return version
def get_load(self):
try:
load = os.getloadavg()[1]
except Exception:
load = "inconnue"
return load
print("sending: " + str(message))
conn.send(message.encode())
conn.close()
def get_os_version():
"Cas des postes pédago"
file_version = "/etc/version"
try:
fd = open(file_version, 'r')
tmp = fd.readline().split('\n')[0]
except Exception:
version = 'inconnue'
else:
version = tmp
return version
def get_load():
try:
load = os.getloadavg()[1]
except Exception:
load = "inconnue"
return load
def usage():
print('Usage: la_commande_sans_argument')
def run(server_class=HTTPServer, handler_class=MyServer, port=PORT):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print("Starting OSInfo [%s] ..." % VERSION)
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 1:
run()
# run()
# run(port=int(argv[1]))
main()
else:
usage()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment