viur.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:transfer 作者: viur-framework 项目源码 文件源码
def genReqStr( params ):
        boundary_str = "---"+''.join( [ random.choice(string.ascii_lowercase+string.ascii_uppercase + string.digits) for x in range(13) ] )
        boundary = boundary_str.encode("UTF-8")
        res = b'Content-Type: multipart/mixed; boundary="'+boundary+b'"\nMIME-Version: 1.0\n'
        res += b'\n--'+boundary
        for(key, value) in list(params.items()):
            if all( [x in dir( value ) for x in ["name", "read", "mimetype"] ] ): #File
                dataVal = value.read()
                type = value.mimetype
                if type=="application/octet-stream":
                    type = magic.from_buffer(dataVal, mime=True)
                res += b'\nContent-Type: '+type.encode("UTF-8")+b'\nMIME-Version: 1.0\nContent-Disposition: form-data; name="'+key.encode("UTF-8")+b'"; filename="'+os.path.basename(value.name).decode(sys.getfilesystemencoding()).encode("UTF-8")+b'"\n\n'
                res += dataVal
                res += b'\n--'+boundary
            elif isinstance( value, list ):
                for val in value:
                    res += b'\nContent-Type: application/octet-stream\nMIME-Version: 1.0\nContent-Disposition: form-data; name="'+key.encode("UTF-8")+b'"\n\n'
                    if isinstance( val, unicode ):
                        res += val.encode("UTF-8")
                    else:
                        res += str(val)
                    res += b'\n--'+boundary
            else:
                res += b'\nContent-Type: application/octet-stream\nMIME-Version: 1.0\nContent-Disposition: form-data; name="'+key.encode("UTF-8")+b'"\n\n'
                if isinstance( value, unicode ):
                    res += unicode( value ).encode("UTF-8")
                else:
                    res += str( value )
                res += b'\n--'+boundary
        res += b'--\n'
        return( res, boundary )
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号