[英]Python “push server” tcp client
I am developing python service for xbmc and I am hopelessly stuck. 我正在为xbmc开发python服务,无可救药。 XBMC has TCP API that communicates by JSON-RPC.
XBMC具有通过JSON-RPC进行通信的TCP API。 XBMC has server TCP socket that is mainly design to recieve commands and respond, but if something happens in system it sends "Notification" to TCP.
XBMC具有服务器TCP套接字,该套接字主要用于接收命令和响应,但是,如果系统中发生了某些情况,它将向TCP发送“通知”。 The problem is that I need to create TCP client that behaves like server therefore it is able to recieve this "Notification".
问题是我需要创建行为类似于服务器的TCP客户端,因此它能够接收此“通知”。 Wherever I run
socket.recv(4096)
it waits for data and stucks my code, because I need to loop my code. 无论我在哪里运行
socket.recv(4096)
它都会等待数据并卡住我的代码,因为我需要循环我的代码。 Structure of code is basically like this: 代码的结构基本上是这样的:
import xbmc, xbmcgui, xbmcaddon
class XPlayer(xbmc.Player) :
def __init__ (self):
xbmc.Player.__init__(self)
def onPlayBackStarted(self):
if xbmc.Player().isPlayingVideo():
doPlayBackStartedStuff()
player=XPlayer()
doStartupStuff()
while (not xbmc.abortRequested):
if xbmc.Player().isPlayingVideo():
doPlayingVideoStuff()
else:
doPlayingEverythingElseStuff()
xbmc.sleep(500)
# This loop is the most essential part of code
if (xbmc.abortRequested):
closeEverything()
xbmc.log('Aborting...')
I tried everything threading, multiprocessing, blocking, non-blocking and nothing helped. 我尝试了所有线程,多处理,阻塞,非阻塞的操作,但没有任何帮助。 Thanks,
谢谢,
You likely want select(), poll() or epoll(): http://docs.python.org/library/select.html 您可能需要select(),poll()或epoll(): http : //docs.python.org/library/select.html
This Python pipe-progress-meter application uses select, as an example: http://stromberg.dnsalias.org/~strombrg/reblock.html 这个Python管道进度表应用程序使用select作为示例: http : //stromberg.dnsalias.org/~strombrg/reblock.html
If you know what sort of delimiters are separating the various portions of the protocol, you may also find this useful, without a need for select or similar: http://stromberg.dnsalias.org/~strombrg/bufsock.html It deals pretty gracefully with "read to the next null byte", "read a maximum of 100 bytes", etc. 如果您知道哪种分隔符可以分隔协议的各个部分,那么您可能会发现它很有用,而无需进行选择或类似操作: http ://stromberg.dnsalias.org/~strombrg/bufsock.html优雅地带有“读取到下一个空字节”,“最多读取100个字节”等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.