hab heute den ganzen Tag vergeblich versucht einen simplen Webserver zum laufen zu bekommen. Wird wohl nur ne Kleinigkeit sein, aber waere sehr froh wenn ihr mir helfen koenntet.
OS: Debian 2.6; Python 2.5.2; GCC 4.3.2;
Hier mein code:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#import socket for network communications
import socket
host = ''
port = 8080
def recvline(conn):
data = ''
while 1:
d = conn.recv(1)
if d == '\n':
break
if d != '\r':
data = data + d
return data
#create socket for IPv4/TCP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#bind the port to the socket
s.bind((host,port))
#listen on the specified port
s.listen(5)
print "The webserver is up and running, listening on port %i." % (port)
while 1:
sock, addr = s.accept()
print "Incomming connection from ", addr
l = recvline(sock)
print "Received from client: ", l
if l == 'QUIT':
break
cmd = l.split()
if cmd[0] == "GET" and len(cmd) == 3:
print "Requested path and file:", cmd[1]
html = "HTTP/1.0 200 OK\n\n"
html += "<html><head><title>TestServer</title></head><body>"
html += "Hello World!"
html += "</body></html>"
sent = sock.send(str(html))
if sent == 0:
print "socket error"
else:
print "it should have worked"
print "Requested data sent."
else:
print "Unknown command."
sock.close()
print "Connection closed."
s.close()
Es scheint als wuerde das HTML nicht durch den Socket geschickt, aber sock.send() ist auch nicht 0, hab wirklich keine Idee mehr woran das noch liegen koennte.