免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6980 | 回复: 3
打印 上一主题 下一主题

python socket accept接受不到? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-16 12:28 |只看该作者 |倒序浏览
本帖最后由 hunter_search 于 2011-06-16 12:36 编辑

[code]
本人python不是很熟悉,问题是这样的,
服务器在做rsync日志的时候,服务器端收不到客户端传送过来的message,  客户端那边netstat -an看到是establelish 但服务器端看到是syn_recv

有高人出来指点一下吗?

下面是主要代码


server 端:

while 1:
        infds,outfds,errfds = select.select([sockobj,],[],[],5)
        data=''
        if len(infds) != 0:
                clientsock,clientaddr = sockobj.accept()
                #print "socketaddr=",sockaddr_in(getsockname())
                _time_now = time.strftime('%Y-%m-%d %X',time.localtime(time.time()))
                print "new client accept...",clientaddr,"####\n"
                os.system("echo '" + _time_now+" "+str(clientaddr)+" accept finished' >> "+socket_log)
        #       child_pid = os.fork()
        #       if child_pid == 0:
        #               socket_task(clientsock)
                #clientsock.setblocking(0)
                clientsock.settimeout(1800)
                cliinfds,clioutfds,clierrfds = select.select([clientsock,],[],[],5)
                if len(cliinfds) :
                        print "new client data comming...."
                        _time_now = time.strftime('%Y-%m-%d %X',time.localtime(time.time()))
                        os.system("echo '" + _time_now+" "+str(clientaddr)+" client data can be read' >> "+socket_log)
                        response=''
                        begin=time.time()
                        Timeout=0
                        while time.time()-begin<=60:
                                print "while begin..."
                                try:
                                        data=clientsock.recv(384)
                                        _time_now = time.strftime('%Y-%m-%d %X',time.localtime(time.time()))
                                        os.system("echo '" + _time_now+" "+str(clientaddr)+" client data ="+data+"' >> "+socket_log)
                                        print "read data =="+data+"###\n"
                                        ##data length must be > 20 character
                                        if len(data)> 20 and re.search("\d+\.\d+\.\d+\.\d+\|\d+\.\d+\.\d+\.\d+.*.tar.gz",data):
                                                response='ok'
                                                clientsock.send(response)
                                                os.system("echo '" + _time_now+" "+str(clientaddr)+" send response ok' >> "+socket_log)
                                                print "send to client data yes###\n"
                                        else:
                                                response='no'
                                                clientsock.send(response)
                                                os.system("echo '"+ _time_now+" "+str(clientaddr)+" send response no' >> "+socket_log)
                                                #clientsock.close()
                                                print "send to client data no###\n"
                                        clientsock.close()
                                        break
                                except:
                                        print "receved error ,socket close ,break"
                                        os.system("echo '" + _time_now+" "+str(clientaddr)+" received error close socket '>> "+socket_log)
                                        clientsock.close()
                                        break
                                time.sleep(0.01)
                                if time.time()-begin>=60:
                                        Timeout=1
                        _time_now = time.strftime('%Y-%m-%d %X',time.localtime(time.time()))
                        if Timeout ==1 :
                                clientsock.send('no')
                                os.system("echo '" + _time_now+" "+str(clientaddr)+" received Timeout  close socket' >> "+socket_log)
                                print "timeout send to client data no###\n"
                        time.sleep(0.2)
                _time_now = time.strftime('%Y-%m-%d %X',time.localtime(time.time()))
                os.system("echo '" + _time_now+" "+str(clientaddr)+" this accept finished socket closing '>> "+socket_log)
                print "new accept finished"
                clientsock.close();
                rsync_task(data)



客户端:

def socket_communicate(other_node,serverPort,messeage,center_log,rsync_error_log,other_log,retry):
        '''
         socket communicate ..
        '''
        #print "socket retry=",retry,"\n"
        _local_times= time.strftime('%Y-%m-%d %X',time.localtime(time.time()))
        sockobj = socket(AF_INET,SOCK_STREAM)
        try:
                sockobj.connect((other_node,serverPort))
                write_file(other_log,_local_times+"send message:"+messeage+" connect  retry: "+str(retry)+" successfully \n",'a')
                #print "connect success"
        except:
                seconds=random.randint(60,180)
                #print "connect failed"
                #seconds=random.randint(5,
                time.sleep(seconds)
                sockobj.close()
                write_file(other_log,_local_times+"send message:"+messeage+" connect exception error retry:"+ str(retry)+"sleep rand "+str(seconds)+" seconds\n",'a')
                retry=retry+1
                return retry
        sockobj.setblocking(0)
        sockobj.send(messeage)
        #print "sendmessage success"
        write_file(other_log,_local_times+"send message:"+messeage+" sendmessage  retry: "+str(retry)+" successfully \n",'a')
        sockobj.settimeout(1800)
                ### socket is noblock ,so the select effect is high
        data=''
        begin=time.time()
        recvtimeout=25
        while 1:
                if data == 'ok':
                        write_file(other_log,_local_times+"send message:"+messeage+" received message: "+data+" retry: "+str(retry)+" successfully \n",'a')
                        print "ok receive response",data,"\n"
                        sockobj.close()
                        return 0
                elif time.time()-begin>=recvtimeout*3:
                        sockobj.close()
                        retry=retry+1
                        write_file(other_log,_local_times+"send message:"+messeage+" received timeout retry:"+str(retry)+" sleep rand "+str(recvtimeout)+"*3 seconds\n",'a')
                        print "receive timeout",data,"\n"
                        return retry

                infds,outfds,errfds = select.select([sockobj,],[],[],5)
                if len(infds) != 0:
                        try:
                                data=sockobj.recv(10)
                                print "select ok  read data=",data,"\n"
                        except:
                                write_file(other_log,_local_times+"receive message:"+messeage+" received error retry:"+str(retry)+" receive error\n",'a')
                                sockobj.close()
                                seconds=random.randint(60,182\)
                                time.sleep(seconds)
                                retry=retry+1
                                return retry
                        _time_now = time.strftime('%Y-%m-%d %X',time.localtime(time.time()))
                        if 'ok' != data:
                                write_file(rsync_error_log, messeage+'\n','a')
                time.sleep(0.1)
        sockobj.close()
        return 0


for i in range(1,RetryCount):
        if retry==0:
                break
        else:
                #print "################################socket_communicate#############"+str(retry)+"\n"
                retry=socket_communicate(other_node,serverPort,item_array,center_log,rsync_error_log,other_log,retry)



[code][

论坛徽章:
0
2 [报告]
发表于 2011-06-16 18:33 |只看该作者
哎,自问自答, 已经搞定了, 将业务抽取出来一个线程处理,将处理socket连接开启一个线程处理!

论坛徽章:
0
3 [报告]
发表于 2011-06-18 09:45 |只看该作者
本帖最后由 106033177 于 2011-06-18 10:10 编辑

回复 1# hunter_search
server端你那是写了些什么?用select了还用到着去开线程吗。

  1. 大概流程
  2. infds = [sockobj,]
  3. while 1:
  4.     readyinfds,readyoutfds,readyerrfds = select.select(ins,[],[],5)
  5.     for s in readyinfds:
  6.         if s ==  sockobj:
  7.             clientsock,clientaddr = sockobj.accept()
  8.             infds.append(clientsock)
  9.         else:
  10.             data=clientsock.recv(384)  
  11.             if len(data)> 20  ....:
  12.                 response='ok'
  13.             else:
  14.                 response='no'
  15.             clientsock.send(response)
  16.             clientsock.close()
  17.             infds.remove(clientsock)
  18.             rsync_task(data)
  19. sockobj.close()
复制代码

论坛徽章:
0
4 [报告]
发表于 2011-06-21 12:05 |只看该作者
回复 3# 106033177


   服务器端一个线程处理socket,一个线程处理任务,如果一个线程同时处理socket和任务的话,不太好吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP