Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的Python套接字只在程序结束时发送数据?_Python_Sockets - Fatal编程技术网

为什么我的Python套接字只在程序结束时发送数据?

为什么我的Python套接字只在程序结束时发送数据?,python,sockets,Python,Sockets,我正在使用Python通过套接字发送信息。我正在使用函数sendall,但直到程序结束,数据才真正发送。当我在发送数据后输入10秒的延迟时,连接另一端的计算机直到延迟结束和程序结束后才接收数据 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(10) conn, addr = s.accept() data = Arbitary conn.sendall(data) #Se

我正在使用Python通过套接字发送信息。我正在使用函数
sendall
,但直到程序结束,数据才真正发送。当我在发送数据后输入10秒的延迟时,连接另一端的计算机直到延迟结束和程序结束后才接收数据

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)
conn, addr = s.accept()

data = Arbitary
conn.sendall(data)   #Send the data
print "This is after sending"
time.sleep(10)
print "This is after delay"
我该怎么做才能让连接另一端的计算机在数据发送后立即接收数据,而不是在Python程序结束时接收数据


我认为没有必要在另一台计算机上添加代码,但如果需要添加,请告诉我。

在我看来,您应该在
conn.close()之后立即用
conn.sendall
关闭Python代码中的连接。可能此数据存储在缓冲区中(连接尚未关闭,因此您可能希望发送更多数据,为什么要浪费资源并持续发送呢?)

仅在脚本关闭时发送数据的原因是套接字会自动刷新和关闭