Last active
February 14, 2024 18:57
-
-
Save maurobaraldi/63c0563a3d376aa4923b556d7b391811 to your computer and use it in GitHub Desktop.
Tests BaseHTTPRequestHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from http.server import HTTPServer, BaseHTTPRequestHandler | |
class HTTPRequestHandler(BaseHTTPRequestHandler): | |
protocol_version = "HTTP/1.0" | |
def do_GET(self): | |
self.send_response(200) | |
self.send_header("Content-Type", "text/plain") | |
self.wfile.write(b"Hello World!\n") | |
self.end_headers | |
if __name__ == "__main__": | |
server = HTTPServer(('', 8080), HTTPRequestHandler) | |
server.serve_forever() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 examples/charpter_2/example1-1.py | |
127.0.0.1 - - [14/Feb/2024 18:46:55] "GET / HTTP/1.1" 200 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import urllib.request | |
>>> r = urllib.request.Request('http://localhost:8080') | |
>>> response = urllib.request.urlopen(r) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
File "/usr/lib/python3.10/urllib/request.py", line 216, in urlopen | |
return opener.open(url, data, timeout) | |
File "/usr/lib/python3.10/urllib/request.py", line 519, in open | |
response = self._open(req, data) | |
File "/usr/lib/python3.10/urllib/request.py", line 536, in _open | |
result = self._call_chain(self.handle_open, protocol, protocol + | |
File "/usr/lib/python3.10/urllib/request.py", line 496, in _call_chain | |
result = func(*args) | |
File "/usr/lib/python3.10/urllib/request.py", line 1377, in http_open | |
return self.do_open(http.client.HTTPConnection, req) | |
File "/usr/lib/python3.10/urllib/request.py", line 1352, in do_open | |
r = h.getresponse() | |
File "/usr/lib/python3.10/http/client.py", line 1375, in getresponse | |
response.begin() | |
File "/usr/lib/python3.10/http/client.py", line 318, in begin | |
version, status, reason = self._read_status() | |
File "/usr/lib/python3.10/http/client.py", line 300, in _read_status | |
raise BadStatusLine(line) | |
http.client.BadStatusLine: Hello World! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ curl -v http://localhost:8080/ | |
* Trying 127.0.0.1:8080... | |
* Connected to localhost (127.0.0.1) port 8080 (#0) | |
> GET / HTTP/1.1 | |
> Host: localhost:8080 | |
> User-Agent: curl/7.81.0 | |
> Accept: */* | |
> | |
* Received HTTP/0.9 when not allowed | |
* Closing connection 0 | |
curl: (1) Received HTTP/0.9 when not allowed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment