-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
35 lines (26 loc) · 865 Bytes
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import requests
import argparse
import getpass
import socket
def get_ip() -> str:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP
def main():
parser = argparse.ArgumentParser(description='ip/username sender\n',
epilog='send ip and username to server')
parser.add_argument('-H', '--host', help="server host")
parsed_args = parser.parse_args()
r = requests.post("http://{host}:1234/".format(host=parsed_args.host),
data={'ip': get_ip(), 'name': getpass.getuser()})
# And done.
print(r.text) # displays the result body.
if __name__ == '__main__':
main()