Last active
December 5, 2016 22:58
-
-
Save knowsuchagency/e854a372bdbae8a85f36c400b6a3dae8 to your computer and use it in GitHub Desktop.
pair client server example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Script started on Mon 05 Dec 2016 02:54:46 PM PST | |
[sfitzpa1@hills pair]$ ls | |
example.script users_client.py users_service.py | |
[sfitzpa1@hills pair]$ echo "Run server in background by appending & to command" && ./users_service.py & | |
[1] 7158 | |
[sfitzpa1@hills pair]$ Run server in background by appending & to command | |
running server: | |
echo "Communicate with server using client script" && ./users_client.py fitz boyd | |
Communicate with server using client script | |
127.0.0.1 - - [05/Dec/2016 14:55:41] "GET /?strings=fitz,boyd HTTP/1.1" 200 - | |
http://localhost:8889?strings=fitz,boyd | |
{'result': ['rfitzge5', | |
'jfitzpa3', | |
'jfitzge8', | |
'sfitzpa1', | |
'dfitzpa2', | |
'gboyd', | |
'gboyd02', | |
'mboyd', | |
'dboyd9']} | |
[sfitzpa1@hills pair]$ ./users_client.py sam | |
127.0.0.1 - - [05/Dec/2016 14:55:52] "GET /?strings=sam HTTP/1.1" 200 - | |
http://localhost:8889?strings=sam | |
{'result': ['lsamuel3', | |
'ssamels', | |
'csample', | |
'msampso3', | |
'hisamali', | |
'asamir', | |
'jsambran', | |
'tonsaman', | |
'gkassame', | |
'lsamuel5', | |
'asamson2', | |
'jsample2']} | |
[sfitzpa1@hills pair]$ exit | |
Script done on Mon 05 Dec 2016 02:55:56 PM PST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from argparse import ArgumentParser | |
from urllib.request import urlopen | |
from pprint import pprint | |
import json | |
parser = ArgumentParser(description= | |
""" | |
A tool to query this Linux machine's /etc/passwd usernames. | |
The path to the server is /students/sfitzpa1/cs232/pair/users_client.py | |
""" | |
) | |
parser.add_argument('strings', nargs='+', help="strings to search this Linux system's users for (i.e: fitz, boyd)") | |
args = parser.parse_args() | |
url = "http://localhost:8889?strings={}".format(','.join(args.strings)) | |
response = urlopen(url) | |
result = json.loads(response.read().decode()) | |
print(url) | |
pprint(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from functools import singledispatch, reduce | |
from urllib.parse import urlparse, parse_qs | |
from operator import add | |
import http.server | |
import json | |
import csv | |
def all_users(): | |
"""Return a list of all users on the system""" | |
with open('/etc/passwd') as fp: | |
reader = csv.reader(fp, delimiter=':') | |
users = [l[0] for l in reader] | |
return users | |
@singledispatch | |
def get_users(): | |
print('requires a string or list of strings') | |
@get_users.register(str) | |
def _(substring): | |
"""Return a list of users on the system that contains the given substring.""" | |
return [u for u in all_users() if substring in u] | |
@get_users.register(list) | |
def _(substrings): | |
"""Return a list of users on the system with any of the given substrings.""" | |
return list(reduce(add, [get_users(s) for s in substrings])) | |
class Handler(http.server.BaseHTTPRequestHandler): | |
def do_GET(self): | |
query = urlparse(self.path).query | |
params = parse_qs(query) | |
if 'strings' in params: | |
strings = params['strings'][0].split(',') | |
result = get_users(strings) | |
self.send_response(200) | |
self.end_headers() | |
self.wfile.write(json.dumps({'result': result}, indent=2).encode()) | |
def run(server_class=http.server.HTTPServer, handler_class=http.server.BaseHTTPRequestHandler, port=8889): | |
server_address = ('', port) | |
httpd = server_class(server_address, handler_class) | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
print('running server:') | |
run(handler_class=Handler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment