diff --git a/solutions/1.py b/solutions/1.py new file mode 100644 index 0000000..284dc82 --- /dev/null +++ b/solutions/1.py @@ -0,0 +1,30 @@ +from io import TextIOWrapper +import socket + +REMOTE = "127.0.0.1" +PORT = 1337 + +def main() -> int: + s = socket.socket() + s.connect((REMOTE, PORT)) + sf: TextIOWrapper = s.makefile('rw') + + _response: str = recv(sf) + + sf.close() + return 0 + +def send(sf: TextIOWrapper, msg: str) -> int: + print(f"> {msg}") + sent: int = sf.write(f"{msg}\n") + sf.flush() + return sent + +def recv(sf: TextIOWrapper) -> str: + response = sf.readline().strip() + print(f"< {response}") + return response + + +if __name__ == "__main__": + exit(main()) diff --git a/solutions/2.py b/solutions/2.py new file mode 100644 index 0000000..c22fe70 --- /dev/null +++ b/solutions/2.py @@ -0,0 +1,32 @@ +from io import BufferedRWPair +import socket + +REMOTE = "127.0.0.1" +PORT = 1337 + +def main() -> int: + s = socket.socket() + s.connect((REMOTE, PORT)) + sf: BufferedRWPair = s.makefile('rwb') + + pl = int.to_bytes(1337, 2, "big") + + _ = send(sf, pl) + + sf.close() + return 0 + +def send(sf: BufferedRWPair, msg: bytes) -> int: + print(f"> {msg.hex()}") + sent: int = sf.write(msg + b"\n") + sf.flush() + return sent + +def recv(sf: BufferedRWPair) -> bytes: + response = sf.readline().strip() + print(f"< {response}") + return response + + +if __name__ == "__main__": + exit(main()) diff --git a/solutions/3.py b/solutions/3.py index 304b947..5f05bdd 100644 --- a/solutions/3.py +++ b/solutions/3.py @@ -60,7 +60,7 @@ def main() -> int: s.close() return 1 - s.close() + sf.close() return 0 def send(sf: TextIOWrapper, msg: str) -> int: