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')

  • 相关阅读:
    MOS管基本构造和工作原理
    压控恒流源电路
    TI博客文章-4-20mA电流环路发送器入门
    node.js发http请求
    node.js之web开发 koa入门
    nodejs入门开发与常用模块
    node.js安装与入门使用
    node.js和前端js有什么区别
    php hash_hmac 与python hmac 区别
    redis命令使用
  • 原文地址:https://www.cnblogs.com/mrchige/p/6222714.html
Copyright © 2011-2022 走看看