[英]Python tcp socket client
I need to have a tcp socket client connected to a server to send data and receive. 我需要有一个连接到服务器的tcp套接字客户端来发送数据和接收。 But this socket must be always on and i cannot open another socket.
但是这个套接字必须始终打开,我无法打开另一个套接字。
I have always some data to send over the time and then later process the answer to the data sent previously. 我总是会有一些数据要随时发送,然后再处理之前发送的数据的答案。
If i could open many sockets, i think it was more easy. 如果我可以打开许多插座,我认为这更容易。 But in my case i have to send everything on the same socket asynchronously.
但在我的情况下,我必须异步发送同一个套接字上的所有内容。
So the question is, what do you recommend to use within the Python ecosystem? 所以问题是,你建议在Python生态系统中使用什么? (twisted, tornado, etc) Should i consider node.js or another option?
(扭曲,龙卷风等)我应该考虑node.js或其他选项?
I highly recommend Twisted for this: 我强烈推荐Twisted为此:
ReconnectingClientFactory
that will deal with disconnections and use exponential backoff, and LoopingCall
makes it easy to implement a heartbeat. ReconnectingClientFactory
可以处理断开连接并使用指数退避,而LoopingCall
可以很容易地实现心跳。 I have a service that is exactly like the one you mention (single login, stays on all the time, processes data). 我有一个与你提到的服务完全一样的服务(单一登录,一直保持,处理数据)。 It's been on for months working like a champ.
它已经持续了几个月像冠军一样工作。
Twisted is possibly hard to get your head around, but the tutorials here are a great start. Twisted可能很难让你理解,但这里的教程是一个很好的开始。 Knowing Twisted will get you far in the long run!
从长远来看,了解Twisted会让你走得更远!
"i have to send everything on the same socket asynchronously" “我必须异步发送同一个套接字上的所有内容”
Add your data to a queue, have a separate thread taking items out of the queue and sending via socket.send()
将数据添加到队列,使用单独的线程将项目从队列中取出并通过
socket.send()
发送
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.