【发布时间】:2019-03-11 20:42:17
【问题描述】:
我已经开始修改一个用 python 制作的示例,以通过 TCP 服务器流式传输计数器的输出。代码下方
import socket
import sys
import time
from thread import *
HOST = '' # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as 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):
#Sending message to connected client
#conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
#infinite loop so that function do not terminate and thread do not end.
count = 0
while True:
count = count + 1
#Receiving from client
#data = conn.recv(1024)
#reply = 'OK...' + data
#if not data:
# break
reply = str(count)+'\n'
print reply
conn.send(reply)
time.sleep(1)
#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()
我想使用我的浏览器从远程 http 客户端获取计数器。我必须等待计数器达到至少 260 个计数,然后才能在浏览器上看到它。在第一次 260 计数之后,一切都在服务器和客户端同步运行。我做了不同的尝试来减少发送的缓冲区大小,但每次一开始都会有很大的延迟。
【问题讨论】:
标签: python sockets http tcp delay