zoukankan      html  css  js  c++  java
  • python3.* socket例子

    On Server:

    # -*- coding: utf-8 -*-
    #this is the server 
    import socket

    if "__main__" == __name__:
        try:
            sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            print("create socket suc!")
            
            sock.bind(('localhost',8008))
            print('bind socket suc!')
     
            sock.listen(5)
            print('listen socket suc!')      
            
        except:
            print("init socket err!")
            
        while True:
            print('listren for client...')
            conn,addr = sock.accept()
            print('get client')
            print(addr)
            
            conn.settimeout(5)
            szBuf = conn.recv(1024)
            byt = 'recv:' + szBuf.decode('gbk')
            print(byt)
            
            if '0' == szBuf:
                conn.send('exit')
            else:
                conn.send('welcome client!')
            
            conn.close()
            print('end of the service')
            

    On Client:

    import socket

    if "__main__" == __name__:


        sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        sock.connect(('localhost',8008))
        sock.send(b'0')
        
        szBuf = sock.recv(1024)
        byt = 'recv:' + szBuf.decode('gbk')
        print(byt)
        
        sock.close()
        print('end of the connecct')

  • 相关阅读:
    Tarjan 算法 自学整理
    POJ 2395 Out of Hay
    Codevs 1557 热浪
    Codevs 2956 排队问题
    Codevs 1005 生日礼物
    集合
    奇怪的函数
    关押罪犯
    搭积木
    大数据
  • 原文地址:https://www.cnblogs.com/mrchige/p/6222714.html
Copyright © 2011-2022 走看看