zoukankan      html  css  js  c++  java
  • socket网络编程

    ###网络编程
    ##发送端
    # import socket ###导入socket
    # socket.TCP/IP  ###定义客户端,定义socket类型
    # connect(a.ip,a.port) ##连接远程机器
    # socket.send(hello)##开始发送
    # socket.recv()  ##然后接收
    # socket.close()##客户端关闭
    
    
    ##接收端 ,也就是服务器端
    # import socket  ####
    # socket.TCP/IP  ###  定义类型
    # listen(0.0.0.0,69696)  ###  监听端口
    # waiting()  ###  等待连接
    # recv()  ###  接收数据
    # send()###  发送数据
    
    
    import socket

    client = socket.socket() ###声明socket类型,同时生成socket连接对象
    client.connect(("localhost",6969))
    while True:
    Inde = input(">>").strip()
    if len(Inde)==0:
    continue
    client.send(Inde.encode("utf-8"))
    data = client.recv(1024)
    print("recv:",data.decode())##decode()转换字符编码

    client.close()
     
    
    
    import socket
    sercer = socket.socket()
    sercer.bind(("localhost",6969))##绑定要监听的端口
    sercer.listen()#监听
    while True:
    conn,addr=sercer.accept()##等待客户端请求 ###在这里会返回两个值,
    ###conn就是客户端连接过来而在服务器端为其生成的一个连接实例
    while True:

    data = conn.recv(1024) ###接收,通过返回的数据接收
    print(data)
    conn.send(data.upper())###发送

    sercer.close()
    ###服务器端
    ####客户端,一下吧服务器端的接收到
    import socket
    
    client = socket.socket()
    # client.connect(("192.168.16.200",9999))
    client.connect(("localhost",9999))
    while True:
        cmd = input(">>:").strip()
        if len(cmd) == 0:
            continue
        client.send(cmd.encode("utf-8"))
        cmd_res_size = client.recv(1024)###接收命令结果的长度
        print("执行命令结果大小:",cmd_res_size)
        received_size = 0
        received_data = b""
        while received_size<int(cmd_res_size.decode()):
    
            data = client.recv(1024)
            received_size += len(data)###每次收到的有可能小于1024,所以必须用len判断
            # print(data.decode())
            # print(received_size)
            received_data += data
        else:
            print("cmd res receive done:",received_size)
            print(received_data.decode())
    
    
    
    
    client.close()
    import socket,os
    
    server = socket.socket()
    server.bind(("localhost",9999))
    server.listen()
    while True:
        conn,addr = server.accept()
        print("new conn:",addr)
        while True:
            print("等待指令")
            data = conn.recv(1024)
            if not data:
                print("客户端已断开")
                break
            print("执行指令:",data)
            cmd_res = os.popen(data.decode()).read()#接收字符串,执行结果也是字符串
            print("before send",len(cmd_res))
            if len(cmd_res) == 0:
                cmd_res= "cmd has no output..."
    #################################################
    conn.send(str(len(cmd_res.encode())).encode("utf-8")) ###先发大小给客户端 ########这两行数据可能会导致粘包的结果,粘包就是说两条数据太快会当作一条数据处理,这时候会报错(纯属瞎扯
    个人想记住而已)
         ###可以在这里加上一个时间沉睡,不过用户体验很不好
         ####time.sleep(0.05)
    #########所以我们可以这样做:
         client_ack= conn.recv(1024)
    #########在这里产生交互,就断开粘包了
    conn.send(cmd_res.encode("utf-8"))
    ################################################
    print("send done") server.close() ###服务器端
    ###ftp传送文件 发送的服务端
    1.读取文件
    2.检测文件是否存在
    3.打开文件
    4.检测文件大小
    4.发送文件大小给客户端
    5.等待客户端确认(也就是怕粘包)
    6.开始边读边发数据
    8.发送md5
    ######
    ###服务端
    import socket,os,time,hashlib
    
    server = socket.socket()
    server.bind(("localhost",9999))
    server.listen()
    
    while True:
        conn,addr = server.accept()
        print("new conn:",addr)
        while True:
            print("等待指令")
            data = conn.recv(1024)
            if not data:
                print("客户端已断开")
                break
            cmd,filename = data.decode().split()
            print(filename)
            if os.path.isfile(filename):###检测是否是文件
                f = open(filename,"rb")###读取文件
                # m = hashlib.md5()
                file_size = os.stat(filename).st_size###获取文件大小
                conn.send(str(file_size).encode())
                conn.recv(1024)
                for line in f:
                    # m.update(line)
                    conn.send(line)
                # print("file md5:",m.hexdigest())
                f.close()
            print("send done")
    sevcer.close()
    
    
    
    #########
    ##客户端
    import socket
    client = socket.socket()
    
    client.connect(("localhost",9999))
    while True:
        cmd = input(">>:").strip()
        if len(cmd) == 0:
            continue
        if cmd.startswith("get"):
            client.send(cmd.encode())
            server_response = client.recv(1024)
            print("servr response:",server_response)
            client.send(b"ready to recv file")
            file_total_size = int(server_response.decode())
            received_size = 0
            filename = cmd.split()[1]
            f = open(filename + "new","wb")
            while received_size<file_total_size:
                data = client.recv(1024)
                received_size += len(data)
                f.write(data)
                # print(data)
            else:
                print("file recv done",received_size,file_total_size)
            f.close()
    client.close()
    ###ftp传送文件 发送的服务端  带md5  加上不会粘包
    
    ####客户端
    import socket
    import hashlib
    client = socket.socket()
    
    client.connect(("localhost",9999))
    while True:
        cmd = input(">>:").strip()
        if len(cmd) == 0:
            continue
        if cmd.startswith("get"):
            client.send(cmd.encode())
            server_response = client.recv(1024)
            print("servr response:",server_response)
            client.send(b"ready to recv file")
            file_total_size = int(server_response.decode())
            received_size = 0
            filename = cmd.split()[1]
            f = open(filename + "new","wb")
            m = hashlib.md5()
            while received_size<file_total_size:
                if file_total_size - received_size>1024:###要收的不止一次
                    size = 1024
                else: ###最后一次了,生多少收多少
                    size = file_total_size - received_size
                data = client.recv(1024)
                received_size += len(data)
                m.update(data)
                f.write(data)
                # print(data)
            else:
                new_file_md5 = m.hexdigest()
                print("file recv done",received_size,file_total_size)
            f.close()
            server_file_md5 = client.recv(1024)
            print("client file md5:",new_file_md5)
    client.close()
    
    
    ###服务端
    
    import socket,os,time,hashlib
    
    server = socket.socket()
    server.bind(("localhost",9999))
    server.listen()
    
    while True:
        conn,addr = server.accept()
        print("new conn:",addr)
        while True:
            print("等待指令")
            data = conn.recv(1024)
            if not data:
                print("客户端已断开")
                break
            cmd,filename = data.decode().split()
            print(filename)
            if os.path.isfile(filename):###检测是否是文件
                f = open(filename,"rb")###读取文件
                m = hashlib.md5()
                file_size = os.stat(filename).st_size###获取文件大小
                conn.send(str(file_size).encode())
                conn.recv(1024)
                for line in f:
                    m.update(line)
                    conn.send(line)
                print("file md5:",m.hexdigest())
                f.close()
                conn.send(m.hexdigest().decode())  ###给客户端发送MD5
            print("send done")
    sevcer.close()
    以上内容作为课堂笔记,如有雷同,请联系于我
  • 相关阅读:
    Spark2.0机器学习系列之12: 线性回归及L1、L2正则化区别与稀疏解
    Spark2.0机器学习系列之11: 聚类(幂迭代聚类, power iteration clustering, PIC)
    mysql 5.7.16多源复制
    Windows版 mysql 5.7.16安装
    一分钟经理人
    转 spring官方文档中文版
    MySQL执行计划 EXPLAIN参数
    sql 转 markdown
    Linux下安装MySQL5.6
    MyBatis完全使用指南
  • 原文地址:https://www.cnblogs.com/ArtisticMonk/p/8989049.html
Copyright © 2011-2022 走看看