Skip to content

Instantly share code, notes, and snippets.

@uchan-nos
Last active August 29, 2015 14:27
Show Gist options
  • Save uchan-nos/29bd38cce5c683bb9439 to your computer and use it in GitHub Desktop.
Save uchan-nos/29bd38cce5c683bb9439 to your computer and use it in GitHub Desktop.
web application for socat command.
#!/usr/bin/python3
import json
import re
from socket import gethostname
from subprocess import check_output
MEM_INFO_PATTERN = re.compile('^([^ ]+): *([0-9]+) ?([A-Za-z]+)?$')
def main():
mem_info_output = check_output(['/bin/cat', '/proc/meminfo'])
mem_info_output = mem_info_output.decode('utf-8')
for line in mem_info_output.splitlines():
m = MEM_INFO_PATTERN.match(line)
if m.group(1) == 'MemFree':
unit = m.group(3)
scale = 1
if unit == 'kB':
scale = 1024
else:
raise ValueError('Unknown unit: ' + unit)
mem_free = int(m.group(2)) * scale
result = json.dumps({
'hostname': gethostname(),
'memfree': mem_free
})
print('HTTP/1.1 200 OK')
print('Content-Type: application/json')
print('Content-Length: {}'.format(len(result) + 1))
print()
print(result)
if __name__ == '__main__':
main()
@uchan-nos
Copy link
Author

socat TCP-LISTEN:8000,fork,reuseaddr EXEC:./webapp.py

のような感じに実行しておいて

curl http://localhost:8000/

という感じにすると実行できます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment