我正在尋找一個標準的Python 2.7包提供一個HTTP服務器,同時執行流連接在同一個端口號。Python 2.7:流HTTP服務器支持一個端口上的多個連接
嘿你是主持人那裏,請停止標記我的問題作爲想要以非流式方式提供服務的問題的副本,例如:Multithreaded web server in python。不,我不想要諸如ThreadingMixIn
這樣的黑客,它只收集響應並將其作爲一個單元返回。
換句話說,我正在尋找標準的方式去做下面的例子程序 - 但是沒有自己編寫整個HTTP服務器。
import time, socket, threading
sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 8000
sock.bind((host, port))
sock.listen(1)
# my OWN HTTP server... Oh man, this is bad style.
HTTP = "HTTP/1.1 200 OK\nContent-Type: text/html; charset=UTF-8\n\n"
class Listener(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.daemon = True # stop Python from biting ctrl-C
self.start()
def run(self):
conn, addr = sock.accept()
conn.send(HTTP)
# serve up an infinite stream
i = 0
while True:
conn.send("%i " % i)
time.sleep(0.1)
i += 1
[Listener() for i in range(100)]
time.sleep(9e9)
所以首先我想:
# run with this command:
# gunicorn -k gevent myapp:app
import time
def app(environ, start_response):
data = b"Hello, World!\n"
start_response("200 OK", [
("Content-Type", "text/plain"),
("Content-Length", str(len(data)))
])
for i in range(5):
time.sleep(1)
yield "Hello %i\n" % i
# https://stackoverflow.com/questions/22739394/streaming-with-gunicorn
但遺憾的是它不流,甚至與-k gevent
。
更新:它似乎gunicorn正在嘗試做Keepalive,這將需要與last-chunk
位的塊傳輸編碼。快速的消息來源顯示它沒有實現這一點。所以我可能需要一個更有趣的HTTP服務器,或者一個更簡單的HTTP服務器(就像我上面的第一個例子,基於socket
),它不會影響keepalive(對於大數據流來說這非常愚蠢)。
於是我嘗試:
import time
import threading
import BaseHTTPServer
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
if self.path != '/':
self.send_error(404, "Object not found")
return
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
# serve up an infinite stream
i = 0
while True:
self.wfile.write("%i " % i)
time.sleep(0.1)
i += 1
class Listener(threading.Thread):
def __init__(self, i):
threading.Thread.__init__(self)
self.i = i
self.daemon = True
self.start()
def run(self):
server_address = ('', 8000+self.i) # How to attach all of them to 8000?
httpd = BaseHTTPServer.HTTPServer(server_address, Handler)
httpd.serve_forever()
[Listener(i) for i in range(100)]
time.sleep(9e9)
這是非常好的,但它是一個有點討厭,我不得不分配100端口號。這將需要一個令人討厭的客戶端重定向來讓瀏覽器進入下一個可用的端口(好吧,我可以用JavaScript隱藏它,但它不夠優雅,我寧願寫自己的HTTP服務器也不會那樣)。
必須有一個簡單的方法才能在一個端口上獲得所有BaseHTTPServer
聽衆,因爲它是設置Web服務器的一種標準方式。或者也許gunicorn
或某些包可以製作可靠的流?
看看[扭曲](https://twistedmatrix.com/) – aergistal
@aergistal嗯,問候*網絡*例如在該網站上是完全不流......如果你搜索該網站您可以獲取實際基於BaseHTTPServer的https://twistedmatrix.com/trac/attachment/ticket/9187/server.py。因此,這只是更確認BaseHTTPServer是否可行。 –