Skip to content

Commit 6e66b73

Browse files
authored
Merge pull request duboviy#2 from extended-debug/master
Merge pull requests (duboviy#1-#4) from remote extended-debug fork
2 parents b48dbcf + 0e60a37 commit 6e66b73

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

get_os_info.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
import time
3+
4+
import psutil # pip install psutil
5+
import humanfriendly # pip install humanfriendly
6+
7+
8+
def get_os_info():
9+
""" Get some OS info with psutils and humanfriendly. """
10+
is_win = lambda: True if os.name == 'nt' else False
11+
pid = os.getgid() if not is_win() else None
12+
ppid = os.getppid()
13+
14+
now = time.time()
15+
16+
current_process = psutil.Process(pid=ppid)
17+
process_uptime = current_process.create_time()
18+
process_uptime_delta = now - process_uptime
19+
process_uptime_human = humanfriendly.format_timespan(process_uptime_delta)
20+
21+
system_uptime = psutil.boot_time()
22+
system_uptime_delta = now - system_uptime
23+
system_uptime_human = humanfriendly.format_timespan(system_uptime_delta)
24+
25+
free_memory = psutil.disk_usage('/').free
26+
total_memory = psutil.disk_usage('/').total
27+
percent_used_memory = psutil.disk_usage('/').percent
28+
used_memory = psutil.disk_usage('/').used
29+
free_memory_human = humanfriendly.format_size(free_memory)
30+
31+
return vars()
32+
33+
34+
if __name__ == '__main__':
35+
from pprint import pprint
36+
pprint(get_os_info())
37+
### Output example:
38+
# {'current_process': <psutil.Process(pid=10628, name='pycharm.exe') at 2074457600744>,
39+
# 'free_memory': 28299886592,
40+
# 'free_memory_human': '28.3 GB',
41+
# 'is_win': <function get_os_info.<locals>.<lambda> at 0x000001E2FFDED158>,
42+
# 'now': 1491982808.197413,
43+
# 'percent_used_memory': 88.2,
44+
# 'pid': 1001,
45+
# 'ppid': 10628,
46+
# 'process_uptime': 1491471889.0,
47+
# 'process_uptime_delta': 510919.1974129677,
48+
# 'process_uptime_human': '5 days, 21 hours and 55 minutes',
49+
# 'system_uptime': 1490198873.0,
50+
# 'system_uptime_delta': 1783935.1974129677,
51+
# 'system_uptime_human': '2 weeks, 6 days and 15 hours',
52+
# 'total_memory': 239530405888,
53+
# 'used_memory': 211230519296}

module_functions_getter.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import operator
2+
import inspect
3+
4+
5+
def get_module_functions(module, function_prefix=''):
6+
7+
def filter_function(inspected_function):
8+
function_name, function = inspected_function
9+
if (function_name.startswith(function_prefix) and
10+
inspect.getmodule(function) == module):
11+
return True
12+
13+
funcs = map(operator.itemgetter(1),
14+
filter(filter_function,
15+
inspect.getmembers(module, inspect.isfunction)))
16+
return funcs

server.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import socket
2+
import sys
3+
4+
HOST = '' # interface
5+
PORT = 8888 # port
6+
7+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8+
print('Socket created')
9+
10+
# Bind socket to local host and port
11+
try:
12+
s.bind((HOST, PORT))
13+
except socket.error as msg:
14+
print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
15+
sys.exit()
16+
17+
print('Socket bind complete')
18+
19+
# Start listening on socket
20+
s.listen(10)
21+
print('Socket now listening')
22+
23+
# now keep talking with the client
24+
while 1:
25+
conn, addr = s.accept()
26+
print('Connected with ' + addr[0] + ':' + str(addr[1]))
27+
conn.send(b'Welcome to the server. Type something and hit enter\n')
28+
while True:
29+
data = conn.recv(1024)
30+
reply = 'Reply text\n'
31+
if not data:
32+
break
33+
34+
conn.sendall(bytes(reply, 'utf-8'))
35+
36+
conn.close()
37+
38+
s.close()
39+
40+
41+
# To test just use:
42+
# >>> telnet localhost 8888

watcher.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import time
2+
from watchdog.observers import Observer
3+
from watchdog.events import PatternMatchingEventHandler
4+
5+
6+
class ScriptModifiedHandler(PatternMatchingEventHandler):
7+
patterns = ['*.py']
8+
9+
def __init__(self):
10+
super(ScriptModifiedHandler, self).__init__()
11+
# you can add some init code here
12+
13+
def process(self, event):
14+
print(event.src_path, event.event_type)
15+
16+
def on_modified(self, event):
17+
self.process(event)
18+
19+
def on_moved(self, event):
20+
pass
21+
22+
def on_deleted(self, event):
23+
pass
24+
25+
def on_created(self, event):
26+
pass
27+
28+
29+
if __name__ == '__main__':
30+
observer = Observer()
31+
path = '.'
32+
event_handler = ScriptModifiedHandler()
33+
observer.schedule(event_handler, path, recursive=True)
34+
observer.start()
35+
try:
36+
while True:
37+
time.sleep(1)
38+
except KeyboardInterrupt:
39+
observer.stop()
40+
observer.join()
41+

0 commit comments

Comments
 (0)