File: server_cpu_local_socket.py

package info (click to toggle)
labplot 2.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 78,528 kB
  • sloc: cpp: 241,047; ansic: 6,324; python: 915; xml: 400; yacc: 237; sh: 221; awk: 35; makefile: 11
file content (24 lines) | stat: -rwxr-xr-x 491 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python

import socket, psutil, os

ADDR = './local_socket'
serv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

if os.path.exists(ADDR):
  print('socket exists. Removing it')
  os.remove(ADDR)

serv.bind(ADDR)
serv.listen(1)

print 'listening ...'

while True:
  conn, addr = serv.accept()
  print 'client connected ... ', addr
  cpu_percent = str(psutil.cpu_percent())
  conn.send(cpu_percent)
  print('written ' + cpu_percent)
  conn.close()
  print('client disconnected')