python socket accept接受不到?
本帖最后由 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][
[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][
作者: hunter_search 发布时间: 2011-06-16
哎,自问自答, 已经搞定了, 将业务抽取出来一个线程处理,将处理socket连接开启一个线程处理!
作者: hunter_search 发布时间: 2011-06-16