Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #18

Closed
wants to merge 11 commits into from
Next Next commit
init
Moist-Cat committed Dec 26, 2024
commit 50649dac6664e9653d6ca47442ad7958bce65098
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# python cache
__pycache__/

# packaging
env/*

# logs
*.log
*.error
*.error.1

# vim
*~
*.swp
*.un~
*.swo
Empty file added src/ptth/__init__.py
Empty file.
67 changes: 67 additions & 0 deletions src/ptth/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import socket

class NotConnected(Exception):
pass

HTTP_PORT = 80

class HTTPConnection:
_http_vsn = 11
_http_vsn_str = 'HTTP/1.1'
default_port = HTTP_PORT

def __init__(self, host: "IP", port: int, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, blocksize=8192):
self.host = host
self.port = port
self.timeout = timeout

# sock
self.sock = None
self.blocksize = blocksize

def connect(self):
self.sock = socket.create_connection((self.host, self.port), self.timeout)

def close(self):
if self.sock:
self.sock.close()
self.sock = None

def send(self, data):
if not self.sock:
raise NotConnected()
print(f"INFO - sending %s" % (data,))

self.sock.sendall(data)

def receive(self):
return self.sock.recv(1024)

def __str__(self):
return f"<({self.__class__.__name__}) [{self.host=}, {self.port=}]>"

__repr__ = __str__


if __name__ == "__main__":
host = "127.0.0.1"
port = 8000

data = "hello world"
request = (
"GET / HTTP/1.1 \r\n"
"Host: localhost\r\n"
"User-Agent: blob\r\n"
f"Content-Length: {len(data)}\r\n"
f"\r\n{data}\r\n"
)

conn = HTTPConnection(host, port)
try:
print(conn)

conn.connect()
conn.send(bytes(request, "utf8"))
print(conn.receive())
finally:
conn.close()
Empty file added src/ptth/server.py
Empty file.