Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- HOST = '127.0.0.1' # Standard loopback interface address (localhost)
- PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
- response = """HTTP/1.1 200 OK
- Content-Type: text/html; charset=utf-8
- <html>
- <head>
- <title>My Great Title</title>
- </head>
- <body>
- <h1>Hello from socket</h1>
- </body>
- </html>
- """
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- s.bind((HOST, PORT))
- while True:
- print("Start listening...")
- s.listen()
- conn, addr = s.accept()
- with conn:
- print('Connected by', addr)
- while True:
- data = conn.recv(1024) # 1010101110101…
- print(data.decode("utf-8"))
- if not data:
- break
- conn.sendall(response.encode("utf-8")) # 110101111001…
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement