分享

python 长连接的socket通信

 心不留意外尘 2016-05-02
http://blog.sina.com.cn/s/blog_6a1837e90100m0xy.html


客户端Client.py:
import socket
import sys
import time  
    
def close_socket(sock) :
    try:
        sock.close()  
    except socket.error, e :    
        print 'Error close socket:%s' % e    
#    else :
#        print 'Close socket successfully!'

def flush_socket(sock_fd) :
    try:
        sock_fd.flush()   
    except socket.error, e:    
        print 'Error flush the buffer:%s' % e    
        return False
    else:
        #print 'Flush the buffer successfully'    
        return True

def communicate(sock):
    while True:
        try:
            #print 'begin to send'
            sock.send('Hello!\r\n')            
            #print 'send ok'
        except socket.error, e:    
            print 'Error sending data:%s' % e
            close_socket(sock)
            return            
        
        if not flush_socket(sock_fd):
            close_socket(sock)
            return
        
        while True:    
            try:
                #print 'begin to rec'                
                buf = sock.recv(2048)
            except socket.error, e:    
                print 'Error receiving data:%s' % e
                close_socket(sock)
                return
            if not len(buf):
                print 'Socket has been closed!'
                close_socket(sock)
                return      
            if '\r\n' in buf : #\r\n is the terminator 
                break             
        
        print 'The receiving data is', buf  
       
        if not flush_socket(sock_fd):
            close_socket(sock)
            return 
            
        time.sleep(1)
    
if __name__ == '__main__':  
    
    try:   
        socket.setdefaulttimeout(3)
    except socket.error, e:
        print 'Strange error setdefaulttimeout:%s' % e 
        sys.exit(1)
    
    while True:
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
            #sock.setblocking(0)
        except socket.error, e:    
            print 'Strange error creating socket:%s' % e 
            continue 
         
        print 'Create socket successfully' 
    
        try:
            sock.connect(('localhost', 8650))
        except socket.error, e:    
            print 'Connection error:%s' % e     
            close_socket(sock)
            continue
        
        print 'Connect successfully'
        
        try:
            sock_fd = sock.makefile('rw', 0)
        except socket.error, e:    
            print 'Makefile error:%s' % e
            close_socket(sock)
            continue
            
        communicate(sock)

------------------------------------------------------------------------------------------------

服务器端Server.py:
import socket
import sys
import time  

def close_socket(sock) :
    try:
        sock.close()  
    except socket.error, e :    
        print 'Error close socket:%s' % e    
    #else :
        #print 'Close socket successfully!'

def flush_socket(sock_fd) :
    try:
        sock_fd.flush()   
    except socket.error, e:    
        print 'Error flush the buffer:%s' % e    
        return False
    else:
        #print 'Flush the buffer successfully'    
        return True
        
def communicate(connection) :
    while True:  
        while True:    
            try:
                #print 'begin to rec'                
                buf = connection.recv(2048)
            except socket.error, e:    
                print 'Error receiving data:%s' % e
                close_socket(connection)
                return
            if not len(buf):
                print 'Socket has been closed!'
                close_socket(connection)
                return          
            if '\r\n' in buf : #\r\n is the terminator 
                break             
        
        print 'The receiving data is', buf  
       
        if not flush_socket(sock_fd):
            close_socket(connection)
            return 
     
        try:
            #print 'begin to send'
            connection.send('Welcome!\r\n')            
            #print 'send ok'
        except socket.error, e:    
            print 'Error sending data:%s' % e
            close_socket(connection)
            return            
        
        if not flush_socket(sock_fd):
            close_socket(connection)
            return 

if __name__ == '__main__':  

    try:   
        socket.setdefaulttimeout(3)
    except socket.error, e:
        print 'Strange error setdefaulttimeout:%s' % e 
        sys.exit(1)         

    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
        #sock.setblocking(0)
    except socket.error, e:    
        print 'Strange error creating socket:%s' % e 
        sys.exit(1) 
     
    print 'Create socket successfully' 
    
    try:
        sock.bind(('localhost', 8650))
    except socket.error, e:    
        print 'Strange error bind socket:%s' % e 
        sys.exit(1) 
        
    print 'Bind successfully'
    
    try:
        sock.listen(5)
    except socket.error, e:    
        print 'Strange error begin to listen:%s' % e 
        sys.exit(1)
      
    print 'Server - Begin to listened'
    
    while True:
        try:
            connection,address = sock.accept() 
        except socket.error, e:    
            print 'Strange error begin to accept:%s' % e 
            continue
        
        try:
            sock_fd = connection.makefile('rw', 0)
        except socket.error, e:    
            print 'Makefile error:%s' % e
            close_socket(connection)
            continue
            
        print 'Server - have accepted'
        
        communicate(connection)
    
    
     
   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多