-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_client4.py
90 lines (77 loc) · 3.59 KB
/
test_client4.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import socket
import select
import sys
import geocoder
import requests
import json
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:
print("Correct usage: script, IP address, port number")
exit()
IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.connect((IP_address, Port))
while True:
sockets_list = [sys.stdin, server]
read_sockets,write_socket, error_socket = select.select(sockets_list, [], [])
# print(read_sockets)
for socks in read_sockets:
if socks == server:
message = socks.recv(2048)
print(message.decode())
else:
message = str(sys.stdin.readline())
if message=="Report Accident\n" or message=="REPORT ACCIDENT\n" or message=="report accident\n" or message=="Report Accident\n":
choice=input("Press '1' to use your location\nPress '2' to manually enter coordinates")
if choice=='1':
while True:
send_url = 'http://freegeoip.net/json'
r = requests.get(send_url)
j = json.loads(r.text)
lat = j['latitude']
lon = j['longitude']
gloc = geocoder.google([lat, lon], method='reverse')
if gloc.ok==True:
break
else:
continue
try:
accident_message="Accident reported at <" +"lat = " + str(lat) +">, <lon = " +str(lon)+">\n" +gloc.city+", "+gloc.state+", "+gloc.country+" \n"
server.send(accident_message.encode())
sys.stdout.write("<You>")
sys.stdout.write(accident_message)
sys.stdout.flush()
except:
print("Connection Failed")
sys.exit(0)
elif choice=='2':
while True:
try:
lat=float(input("Please enter latitude in range 41 - 47"))
lon=float(input("Please enter longitude -71 - -77"))
break
except:
print("Invalid Input...Try again")
continue
gloc = geocoder.google([lat, lon], method='reverse')
try:
if gloc.ok==False:
print("Unable to fetch location for given coordinates")
else:
accident_message="Accident reported at <" +"lat = " + str(lat) +">, <lon = " +str(lon)+">\n" +gloc.city+", "+gloc.state+", "+gloc.country+" \n"
server.send(accident_message.encode())
sys.stdout.write("<You>")
sys.stdout.write(accident_message)
sys.stdout.flush()
except:
print("Connection Failed")
sys.exit(0)
else:
print("Invalid Input...Try again")
continue
else:
server.send(message.encode())
sys.stdout.write("<You>")
sys.stdout.write(message)
sys.stdout.flush()
server.close()