Reputation: 545
I have been trying to connect to the google-geocoder api through lower-level socket module with ssl encryption. In the place API_key_here, I actually have my own API in the script.
Every time I execute the script I get the following error from ssl "ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:847)" I am running Linux Mint and Python is 3.6.
import socket
from urllib.parse import quote_plus
import ssl
key = "API_key_here"
text = """\
GET /maps/api/geocode/json?key={}&address={}&sensor=false HTTP/1.1\r\n
Host: maps.google.com:80\r\n
User-Agent: 1-4-socket-geocoding-py-network.py\r\n
Connection: close\r\n
"""
def geocode(address):
sock = socket.socket()
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
sock_ssled = context.wrap_socket(sock, server_hostname="maps.google.com")
print(sock_ssled.version)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
sock_ssled.connect(("maps.google.com", 80))
request = text.format(key, quote_plus(address))
sock_ssled.sendall(request.encode("ascii"))
rawreply =b""
while True:
more = sock_ssled.recv(4096)
if not more:
break
rawreply += more
print (rawreply.decode("utf-8"))
if __name__ == "__main__":
geocode ('207 N. Defiance St, Archbold, OH')
below are the error output
<bound method SSLSocket.version of <ssl.SSLSocket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>>
Traceback (most recent call last):
File "/home/user1/pyenv1/scripts/1-4-socket-geocoding-py-network.py", line 36, in <module>
geocode ('207 N. Defiance St, Archbold, OH')
File "/home/user1/pyenv1/scripts/1-4-socket-geocoding-py-network.py", line 23, in geocode
sock_ssled.connect(("maps.google.com", 80))
File "/usr/lib/python3.6/ssl.py", line 1109, in connect
self._real_connect(addr, False)
File "/usr/lib/python3.6/ssl.py", line 1100, in _real_connect
self.do_handshake()
File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:847)
Upvotes: 1
Views: 8135
Reputation: 4016
You should probably connect to port 443 instead of 80. 80 is for plain HTTP, 443 is for HTTPS.
Upvotes: 2