Reputation: 109
For a peice of work I have created a web server that handles multiple requests with threading however it now hangs when I run the program and I just cant work out why. It never reaches the stage of print('Connected by', address). Any help and explination would be greatly appreciated.
class Connect(threading.Thread):
def __init__ (self, connection):
self.clientsocket = connection
threading.Thread.__init__(self)
def run(self):
stream = connection.makefile(mode="rw", buffering=1, encoding="utf-8")
firstLine = stream.readline().split(" ")
hList = []
method = firstLine[0]
path = firstLine[1]
line = stream.readline().strip()
while line != "":
hList.append(line.split(":", 1))
line = stream.readline().strip()
if method != 'GET':
stream.write("HTTP/1.0 405 Unsupported\n\nUnsupported")
else:
stream.write("HTTP/1.0 200 Success\n")
stream.write("Content-type: text/plain\n")
stream.write("\n")
stream.write(str(firstLine) + '\n')
for header in hList:
stream.write(str(header) + "\n")
stream.close()
connection.close()
return path == "/stop"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 9999))
s.listen(1)
while 1:
connection, address = s.accept()
print('Connected by', address),
Connect(connection).start()
Cheers
Upvotes: 0
Views: 689
Reputation: 53839
Are you running your example with Python 2 instead of Python 3? In Python 2 socket.makefile
does not have a buffering
keyword argument. Your example works fine for me in Python 3.
Upvotes: 1