我尝试使用" persistent"在http上发送图像流。连接。我在标题中保持活着,客户端(firefox)也是如此,但我不知道如何发送第二个请求或如何包装它。
我已经阅读了rfc。我也在查看流媒体服务时就像我试图复制的服务一样。我没有看到任何http消息在数据流传输时来回传播(mjpg流媒体)
所以我的问题是,在http持久连接中发送回客户端需要什么?如果我只是发送完全形成的http响应或只是jpg数据,客户端断开连接。如果我等待客户端在发送第二个映像之前发送更多数据,则客户端永远不会发送任何内容。
import socket
import sys
import time
import binascii
from thread import *
HOST = '' # Symbolic name meaning all available interfaces
PORT = 9998 # Arbitrary non-privileged port
in_file = open("picture.jpg", "rb") # opening for [r]eading as [b]inary
testJpg = in_file.read() # if you only wanted to read 512 bytes, do .read(512)
in_file.close()
in_file = open("picture2.jpg", "rb") # opening for [r]eading as [b]inary
testJpg2 = in_file.read() # if you only wanted to read 512 bytes, do .read(512)
in_file.close()
OddNumber = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#Function for handling connections. This will be used to create threads
def clientthread(conn):
global OddNumber, testJpg, testJpg2
OddNumber = 0
# get data from the client (assume its a http get request)
data = conn.recv(1024)
print("\r\n Got1: "+str(len(data)))
#time.sleep(.05)
while OddNumber < 30:
# create a new reply
reply = "HTTP/1.0 200 OK\r\n"
reply += "Server: MJPG-Streamer/0.2\r\n"
reply += "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n"
reply += "Pragma: no-cache\r\n"
reply += "Content-type: image/jpeg\r\n"
# add in the content len
if OddNumber % 2 == 0:
reply += "Content-Length: "+str(len(testJpg))+"\r\n"
else:
reply += "Content-Length: "+str(len(testJpg2))+"\r\n"
# add the keep alive portion
reply += "Connection: Keep-Alive\r\n"
# I think we need this
reply += "\r\n"
# I use this toggle to switch between different images so if it does "stream" i'll be able to see it
# flipping these images
if OddNumber % 2 == 0:
reply += testJpg
else:
reply += testJpg2
# after the image
reply += "\r\n"
conn.sendall(reply)
OddNumber += 1
print("\r\n OddNum: "+str(OddNumber))
data = conn.recv(1024)
print("\r\n Got1: "+str(len(data)))
print(str(data))
#time.sleep(.05)
#came out of loop
conn.close()
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,))
s.close()
除此之外,我想知道持久连接是否对我有效。我正在开发一个托管图像的嵌入式设备,类似于ipcamera。但我需要能够在向我发送图像时将不同的URL发送回设备。这可能在同一个端口上吗?
有人要求我在我的http回复中显示什么。这是你在python脚本中看到的。它是一个有效的http响应,如果我设置它可以正常工作 连接:Keep-Alive \ r \ n&#34; 至 连接:关闭\ r \ n&#34;
我想我的问题是,你在持久连接中需要做些什么。客户会发送更多&#34; get&#34;请求或是服务器预期流式传输某些数据?至少firefox只发送一个&#34; get&#34;请求,然后我发送带有图像的响应,但接下来发生了什么以保持它?
感谢目前为止提供的所有帮助。
答案 0 :(得分:0)
我担心这是因为你在客户端和服务器中使用相同的套接字对象。你能把代码分成两个文件吗?一个用于服务器的文件和另一个用于客户端的文件(表示你的clientthread函数)。
在HTTP中,当客户端使用'keep-alive'获取HTTP响应时,无论您发送什么类型的数据,它都将保持TCP连接。