zoukankan      html  css  js  c++  java
  • python note 26 socket

    1、server

    import socket
    sock = socket.socket()
    sock.bind(('127.0.0.1',8000))
    sock.listen(5)
    
    while 1:
        conn,addr = sock.accept()
        while 1:
            try:
                data = conn.recv(1024).decode("utf-8")
                user,pwd = data.strip().split("|")
    
                flag = False
                with open("account","r") as f:
                    for line in f:
                        print(line.strip().split(":"))
                        username,password = line.strip().split(":")
                        if username == user and password == pwd :
                            flag = True
                            break
    
                if flag:
                    conn.send(b"success")
                else:
                    conn.send(b"fail")
            except Exception as e:
                break

    2、client

    import socket
    sock = socket.socket()
    sock.connect(('127.0.0.1',8000))
    
    while 1:
        user = input('用户名>>>')
        pwd = input('密码>>>')
        val = ("%s|%s" %(user,pwd)).encode("utf-8")
        sock.send(val)
        res = sock.recv(1024)
        print(res.decode("utf-8"))
        if res.decode("utf-8") == "success":
            break
        else:
            continue
  • 相关阅读:
    显而易见的python
    GitHub 使用教程图文详解
    linux下搭建hexo环境
    linux 删除软链接
    Django2.1入门教程
    windows下安装PyQt4
    python3 Flask安装
    学习之源
    白话C++系列教程
    面试笔试试题精选
  • 原文地址:https://www.cnblogs.com/P-Z-W/p/11138829.html
Copyright © 2011-2022 走看看