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
原文地址:https://www.cnblogs.com/ezway/p/6798346.html