Mit Python3 urlopen aufrufen

  • Python

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von slice.

    Mit Python3 urlopen aufrufen

    Hallo zusammen

    import urllib.request
    x = urllib.request.urlopen('https://www.google.de/')
    print(x.read())

    Ich rufe das Programm mit sudo python3 xyz.py auf.
    Das Programm läuft ohne Fehler durch.

    import urllib.request
    x = urllib.request.urlopen("http://192.168.1.11")
    print(x.read())

    Wenn ich aber jetzt www.google.de mit 192.168.1.11 tausche ist eine IP von ESP 8266 bekomme ich eine Fehler Meldung.
    Die IP wird richtig im Edge Browser richtig angezeigt.

    Traceback (most recent call last):

    Wo ist das Problem mit der IP?
    Danke für die Antwort!!!
    Danke für die Hilfe

    Traceback (most recent call last):
    File "abc_lesen.py", line 3, in <module>
    x = urllib.request.urlopen("http://192.168.1.11")
    File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
    File "/usr/lib/python3.5/urllib/request.py", line 466, in open
    response = self._open(req, data)
    File "/usr/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)
    File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
    File "/usr/lib/python3.5/urllib/request.py", line 1282, in http_open
    return self.do_open(http.client.HTTPConnection, req)
    File "/usr/lib/python3.5/urllib/request.py", line 1257, in do_open
    r = h.getresponse()
    File "/usr/lib/python3.5/http/client.py", line 1198, in getresponse
    response.begin()
    File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
    File "/usr/lib/python3.5/http/client.py", line 279, in _read_status
    raise BadStatusLine(line)
    http.client.BadStatusLine: <!DOCTYPE HTML><html><body><h1>LED schalten</h1><br>led1 = off<br>led2 = on<br>led3 = off</body></html>


    mit urllib2 bin ich auch nicht weiter gekommen
    Ich vermute mal dein ESP 8266 gibt kaputtes HTTP zurück:

    Quellcode

    1. exception http.client.BadStatusLine
    2. A subclass of HTTPException. Raised if a server responds with a HTTP status code that we don’t understand.


    <!DOCTYPE HTML><html><body><h1>LED schalten</h1><br>led1 = off<br>led2 = on<br>led3 = off</body></html> ist definitiv kein valider HTTP-Statuscode.
    So wie es im HTTP spezifiziert ist?

    RFC7230
    Wiki - Response message

    Quellcode: Beispiel

    1. HTTP/1.1 200 OK
    2. Date: Mon, 23 May 2005 22:38:34 GMT
    3. Content-Type: text/html; charset=UTF-8
    4. Content-Length: 138
    5. Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
    6. Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
    7. ETag: "3f80f-1b6-3e1cb03b"
    8. Accept-Ranges: bytes
    9. Connection: close
    10. <html>
    11. <head>
    12. <title>An Example Page</title>
    13. </head>
    14. <body>
    15. Hello World, this is a very simple HTML document.
    16. </body>
    17. </html>