-
-
Notifications
You must be signed in to change notification settings - Fork 641
/
search4.py
executable file
·35 lines (30 loc) · 1.06 KB
/
search4.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
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter01/search4.py
# (The Google API originally used in this example now requires API keys,
# so here's an alternative that calls openstreetmap.org.)
import socket
import ssl
from urllib.parse import quote_plus
request_text = """\
GET /search?q={}&format=json HTTP/1.1\r\n\
Host: nominatim.openstreetmap.org\r\n\
User-Agent: Foundations of Python Network Programming example search4.py\r\n\
Connection: close\r\n\
\r\n\
"""
def geocode(address):
unencrypted_sock = socket.socket()
unencrypted_sock.connect(('nominatim.openstreetmap.org', 443))
sock = ssl.wrap_socket(unencrypted_sock)
request = request_text.format(quote_plus(address))
sock.sendall(request.encode('ascii'))
raw_reply = b''
while True:
more = sock.recv(4096)
if not more:
break
raw_reply += more
print(raw_reply.decode('utf-8'))
if __name__ == '__main__':
geocode('207 N. Defiance St, Archbold, OH')