[英]Publishing data via socket after connection is aborted
I have a Python (2.7) script which is reading realtime data from a file and publishing it (via network) to a server living on a different computer. 我有一个Python(2.7)脚本,该脚本正在从文件中读取实时数据并将其(通过网络)发布到另一台计算机上的服务器。 This server, in particular, is a
Carbon
server part of graphite . 该服务器特别是石墨的
Carbon
服务器部分。
The relevant part of the code is as follows: 该代码的相关部分如下:
import socket
CARBON_HOST = 'COMPUTER-NAME'
CARBON-PORT = 2003
CARBON_PATH = 'folder.name.meaurement'
s = socket.socket()
s.connect((CARBON_HOST, CARBON_PORT))
while True:
if s:
s.send('%s %s %s\n'%(CARBON_PATH, str(data), int(time.time())))
time.sleep(WAIT)
where data
is the latest entry imported from my file, and time
is the usual. data
是从我的文件导入的最新条目,而time
是通常的time
。
When I switch off the computer COMPUTER-NAME
where the Carbon
server lives, this error appears: 当我关闭
Carbon
服务器所在的计算机COMPUTER-NAME
,出现此错误:
s.send('%s %s %s\\n'%(CARBON_PATH, str(data), int(time.time())))
s.send('%s%s%s \\ n'%(CARBON_PATH,str(data),int(time.time())))
socket.error: [Errno 10053] An established connection was aborted by the software in your host machinesocket.error:[Errno 10053]主机中的软件中止了已建立的连接
When I restart the host machine ( COMPUTER-NAME
), I have to restart the Python script for data to be sent over again. 重新启动主机(
COMPUTER-NAME
)时,必须重新启动Python脚本才能再次发送数据。
Is there a way I can tell the socket
to pause if it sees it's disconnected, or to keep trying until the connection is open again? 有没有一种方法可以告诉
socket
在断开连接时暂停,或者继续尝试直到连接再次打开?
I recommend to read about socket.timeout("seconds to sleep") which will give you idea of using it. 我建议阅读有关socket.timeout(“ seconds to sleep”)的信息,这将使您有使用它的想法。 In your case, use socket.settimeout() right before making a socket connection, and socket.settimeout(None) soon after socket.connection().
在您的情况下,请在建立套接字连接之前立即使用socket.settimeout(),然后在socket.connection()之后立即使用socket.settimeout(None)。 In this way, you can delay establishing connection.
这样,您可以延迟建立连接。 But if timeout value goes beyond system/server down time, then script will eventually come out with same timeout error.
但是,如果超时值超出系统/服务器停机时间,则脚本最终将出现相同的超时错误。
connect_timeout = 100 #in seconds
socket.settimeout(connect_timeout)
socket.connect()
socket.settimeout(None)
Check if that works? 检查是否可行?
You can't use the same socket after a socket.error
exception, the connection is broken. 在
socket.error
异常之后,您不能使用同一套接字,连接断开。 However, you can catch the exception, create a new connection, and use that to send the data. 但是,您可以捕获异常,创建新的连接,然后使用该连接发送数据。
About your last question, you can tell your program to keep trying until the data is sent with a while loop. 关于最后一个问题,您可以告诉程序继续尝试,直到使用while循环发送数据为止。 A basic example,
一个基本的例子
import socket
import time
CARBON_HOST = 'COMPUTER-NAME'
CARBON_PORT = 2003
CARBON_PATH = 'folder.name.meaurement'
WAIT = 10
s = socket.socket()
s.connect((CARBON_HOST, CARBON_PORT))
data = 'my data'
while True:
packet = '%s %s %s'%(CARBON_PATH, str(data), int(time.time()))
while True:
try:
s.send(packet)
break
except socket.error as e:
print(e)
s.close()
s = socket.socket()
s.connect_ex((CARBON_HOST, CARBON_PORT))
time.sleep(WAIT)
s.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.