Zum Inhalt springen
ecdsa.com
Python ssl module (OpenSSL)Python cryptography

Fehlermeldung

[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol

Auch ausgegeben als

  • ssl.SSLEOFError: EOF occurred in violation of protocol

Mit OpenSSL 3 zeigt Python den Reason-Code „[SSL: UNEXPECTED_EOF_WHILE_READING]“; mit OpenSSL 1.1.1 lautet der Text nur „EOF occurred in violation of protocol“. Danach folgt eine Quellcode-Position wie „(_ssl.c:1082)“.

Python ssl module (OpenSSL) — ssl.SSLEOFError, SSL_R_UNEXPECTED_EOF_WHILE_READING

Was der Fehler bedeutet

Die Gegenstelle hat die TCP-Verbindung geschlossen, während Python noch im TLS-Handshake war (oder gerade Daten las), ohne einen TLS-Alert oder close_notify zu senden. OpenSSL wertet dieses stille Schließen als Protokollverletzung. Python kann nicht wissen, warum die Gegenstelle die Verbindung beendet hat; der Grund liegt beim Server, bei einem Proxy oder bei einer Firewall dazwischen.

Warum der Fehler auftritt

So behebst du den Fehler

  1. 1.

    Handshake mit openssl s_client nachstellen

    Wird auch s_client getrennt, variiere SNI und die TLS-Version, um zu sehen, woran sich der Server stört; funktioniert s_client, vergleiche seine Einstellungen mit deinem Python-Client (Proxy, SNI, Versionen).

    bash
    # Does a plain OpenSSL handshake work from the same machine?
    openssl s_client -connect api.example.com:443 -servername api.example.com -brief </dev/null
    # Without SNI, and pinned to TLS 1.2, to see what the server objects to
    openssl s_client -connect api.example.com:443 -noservername -brief </dev/null
    openssl s_client -connect api.example.com:443 -servername api.example.com -tls1_2 -brief </dev/null
  2. 2.

    Proxy-Umgebungsvariablen prüfen

    requests, urllib und pip berücksichtigen HTTPS_PROXY und NO_PROXY. Korrigiere das Schema der Proxy-URL und nimm interne Hosts aus, die direkt erreicht werden müssen.

    bash
    env | grep -i -E '^(https?|no|all)_proxy='
    # Proxy URLs should normally be http:// (CONNECT tunnel), and internal hosts listed in NO_PROXY
    export HTTPS_PROXY=http://proxy.example.net:3128
    export NO_PROXY=localhost,127.0.0.1,.internal.example.com
  3. 3.

    Wiederholen, wenn die Ursache vorübergehend ist

    Bei Rate Limiting und instabilen Netzwerken machen Wiederholungen mit Backoff aus sporadischen Resets langsamere Erfolge; einen Server, der den Handshake immer ablehnt, reparieren sie nicht.

    python
    import requests
    from requests.adapters import HTTPAdapter
    from urllib3.util.retry import Retry
    
    session = requests.Session()
    session.mount("https://", HTTPAdapter(max_retries=Retry(total=3, backoff_factor=1)))
    session.get("https://api.example.com/")

Verwandte Fehler

Gesamte Signaturfehler-Datenbank durchsuchen