Version 2

This commit is contained in:
ViesHaem 2024-04-15 19:36:45 +03:00
commit dfe58749c2
2 changed files with 38 additions and 0 deletions

14
socket_client.py Normal file
View File

@ -0,0 +1,14 @@
import socket
HOST = 'localhost'
PORT = 8081
def http_client():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
client.connect((HOST, PORT))
client.sendall(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
response = client.recv(1024)
print('Response:', response.decode('utf-8'))
if __name__ == '__main__':
http_client()

24
socket_server.py Normal file
View File

@ -0,0 +1,24 @@
import socket
PORT = 8081
HOST = '0.0.0.0'
def http_server():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
server.bind((HOST, PORT))
print(f"Server running on {HOST}:{PORT}")
server.listen()
while True:
conn, addr = server.accept()
with conn:
print('Connected by', addr)
request = conn.recv(1024)
print('Request:', request.decode('utf-8'))
response = b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nHello, world'
conn.sendall(response)
if __name__ == '__main__':
http_server()