试图创建一个线程化的 http 服务器。当我通过网络浏览器连接到该服务器时,虽然我只能在服务器停止之前拉起一件事。发生这种情况有什么特别的原因吗?
import socket
from threading import Thread
class http():
def __call__():
return 0
def site(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost", 5124))
s.listen(2)
except socket.error, (value,message):
if s:
s.close()
return 2
c, addr = s.accept()
s = c.recv(1024)
print s
c.send("test")
c.close()
return s
serve = http()
def happy():
while True:
serve.site()
if __name__ == '__main__':
Thread(target = happy()).start()