Version 2
This commit is contained in:
commit
dfe58749c2
14
socket_client.py
Normal file
14
socket_client.py
Normal 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
24
socket_server.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user