python错误的数字ssl

时间:2019-01-11 14:43:29

标签: python sockets ssl

我一直在尝试通过具有ssl加密的低级套接字模块连接到google-geocoder api。在API_key_here位置,我实际上在脚本中有自己的API。

每次执行脚本时,都会从ssl中收到以下错误“ ssl.SSLError:[SSL:WRONG_VERSION_NUMBER]错误的版本号(_ssl.c:847)” 我正在运行Linux Mint,Python是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')

下面是错误输出

    <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)

1 个答案:

答案 0 :(得分:2)

您可能应该连接到端口443而不是80。80用于纯HTTP,443用于HTTPS。