mirror of https://github.com/nocturn9x/giambio.git
Made the socket_ssl test look nicer
This commit is contained in:
parent
f7fbad931a
commit
cd2a436d3d
|
@ -1,9 +1,15 @@
|
|||
from debugger import Debugger
|
||||
import email
|
||||
from io import StringIO
|
||||
import giambio
|
||||
import socket as sock
|
||||
import ssl
|
||||
import sys
|
||||
import time
|
||||
|
||||
_print = print
|
||||
|
||||
def print(*args, **kwargs):
|
||||
sys.stdout.write(f"[{time.strftime('%H:%M:%S')}] ")
|
||||
_print(*args, **kwargs)
|
||||
|
||||
|
||||
async def test(host: str, port: int, bufsize: int = 4096):
|
||||
|
@ -26,6 +32,7 @@ async def test(host: str, port: int, bufsize: int = 4096):
|
|||
async with giambio.skip_after(2) as p:
|
||||
print(f"Pool with {p.timeout - giambio.clock():.2f} seconds timeout created")
|
||||
async with socket:
|
||||
# Closes the socket automatically
|
||||
print("Entered socket context manager, sending request data")
|
||||
await socket.send_all(b"""GET / HTTP/1.1\r\nHost: google.com\r\nUser-Agent: owo\r\nAccept: text/html\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n""")
|
||||
print("Data sent")
|
||||
|
@ -41,9 +48,26 @@ async def test(host: str, port: int, bufsize: int = 4096):
|
|||
break
|
||||
print(f"Request has{' not' if not p.timed_out else ''} timed out!")
|
||||
if buffer:
|
||||
print(f"HTTP Response below {'(might be incomplete)' if p.timed_out else ''}\n")
|
||||
print("\n".join(buffer.decode().split("\r\n")))
|
||||
data = buffer.decode().split("\r\n")
|
||||
print(f"HTTP Response below {'(might be incomplete)' if p.timed_out else ''}")
|
||||
_print(f"Response: {data[0]}")
|
||||
_print("Headers:")
|
||||
content = False
|
||||
for i, element in enumerate(data):
|
||||
if i == 0:
|
||||
continue
|
||||
else:
|
||||
if not element.strip() and not content:
|
||||
# This only works because google sends a newline
|
||||
# before the content
|
||||
sys.stdout.write("\nContent:")
|
||||
content = True
|
||||
if not content:
|
||||
_print(f"\t{element}")
|
||||
else:
|
||||
for line in element.split("\n"):
|
||||
_print(f"\t{line}")
|
||||
|
||||
|
||||
giambio.run(test, "google.com", 443, debugger=())
|
||||
giambio.run(test, "google.com", 443, 256, debugger=())
|
||||
|
||||
|
|
Loading…
Reference in New Issue