• socket post


    c

    #! bin/usr/evn python
    # -*- coding:utf-8 -*-
    import socket,os
    sk=socket.socket()
    sk.connect(('127.0.0.1',8888))
    BASE_DIR=os.path.dirname(__file__)
    while 1:
        ipt=input('>>>').strip()
        cmd,filename=ipt.split('|')
        path=os.path.join(BASE_DIR,filename)
        print(path)
        file_size=os.stat(path).st_size
        # file_info='{:s}{:s}{:d}'.format(cmd,filename,file_size)
        file_info='%s%s%s'% (cmd,filename,file_size)
        sk.sendall(bytes(file_info,'utf8'))
        with open(filename,'rb') as rf:
            has_sent=0
            while has_sent!=file_size:
                ret=rf.read(1024)
                sk.sendall(ret)
                has_sent+=len(ret)
            print('done!')
    
     # rf=open(filename,'rb')
     #    has_sent=0
     #    while has_sent!=file_size:
     #        ret=rf.read(1024)
     #        sk.sendall(ret)
     #        has_sent+=len(ret)
     #
     #    rf.close()
    View Code

    s

    #! bin/usr/evn python
    # -*- coding:utf-8 -*-
    
    import socket,os
    sk=socket.socket()
    sk.bind(('127.0.0.1',8888))
    sk.listen(3)
    BASE_DIR=os.path.dirname(__file__)
    while 1:
        con,addrs=sk.accept()
        while 1:
            file_info=con.recv(1024)
            cmd,filename,file_size=bytes(file_info,'utf8').split('|')
            has_recv=0
            data=bytes()
            while file_size!=has_recv:
                a=con.recv(1024)
                data+=a
                has_recv+=len(a)
            with open(filename,'wb') as wf:
                wf.write(data)
    
    sk.close()
    View Code
  • 相关阅读:
    python 代码片段8
    python 代码片段7
    python 代码片段6
    python 代码片段5
    python 代码片段4
    django 代码片段3
    python 代码片段2
    Redis事物
    Redis的java客户端jedis
    Redis五大数据类型
  • 原文地址:https://www.cnblogs.com/ezway/p/6798346.html
Copyright © 2020-2023  润新知