这样的socket服务算是启动了吗?
端口是9500,我发现服务端隔一段时间就连接上后没反应,重启就正常了,是能够连接的,但是没反应,也没报错,连接上的话会有消息发回来的,我用twisted写的
下面是我的代码
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import json,time
from twisted.internet import reactor
from twisted.internet import protocol
from mysqlClass import mysqlClass
from function import *
##################################################
class TCPServer(protocol.Protocol):
def dataReceived(self, data):
try:
self.do(data)
except Exception, e:
print e
def do(self, data):
handle = self.transport.getHandle()
DB = mysqlClass()
kv = json.loads(data)
if kv.has_key("checkConnection"):
handle.sendall(data)
client = kv["client"]
while True:
now = getDateTime(-5)
sql = "select id,dryFlag as flag,DryValue as value from operate_log where dryClient='%s' and dryStatus=0 and dryAddTime>='%s' limit 10" % (client,now)
dataList = DB.fetchall(sql)
result = []
if len(dataList):
dataIn = []
for rs in dataList:
idString = str(rs["id"])
dataIn.append(idString)
result.append({"id":idString,"flag":rs["flag"],"value":rs["value"]})
dataIn = ",".join(dataIn)
sql = "update operate_log set dryStatus=1 where id in (%s)" % dataIn
DB.execute(sql)
result = json.dumps(result)
handle.sendall(result)
time.sleep(1)
##################################################
if __name__ == "__main__":
factory = protocol.ServerFactory()
factory.protocol = TCPServer
reactor.listenTCP(9500, factory, interface="0.0.0.0")
reactor.run()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论