forked from CursosWeb/X-Serv-14.1-WebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservidor-http-simple.py
36 lines (28 loc) · 1003 Bytes
/
servidor-http-simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python
"""
Simple HTTP Server
Jesus M. Gonzalez-Barahona and Gregorio Robles
{jgb, grex} @ gsyc.es
TSAI, SAT and SARO subjects (Universidad Rey Juan Carlos)
"""
import socket
# Create a TCP objet socket and bind it to a port
# We bind to 'localhost', therefore only accepts connections from the
# same machine
# Port should be 80, but since it needs root privileges,
# let's use one above 1024
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind(('localhost', 1234))
# Queue a maximum of 5 TCP connection requests
mySocket.listen(5)
# Accept connections, read incoming data, and answer back an HTML page
# (in an infinite loop)
while True:
print 'Waiting for connections'
(recvSocket, address) = mySocket.accept()
print 'HTTP request received:'
print recvSocket.recv(1024)
recvSocket.send("HTTP/1.1 200 OK\r\n\r\n" +
"<html><body><h1>Hello World!</h1></body></html>" +
"\r\n")
recvSocket.close()