当前位置: 首页>>代码示例>>Python>>正文


Python Response.status_string方法代码示例

本文整理汇总了Python中pyramid.response.Response.status_string方法的典型用法代码示例。如果您正苦于以下问题:Python Response.status_string方法的具体用法?Python Response.status_string怎么用?Python Response.status_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyramid.response.Response的用法示例。


在下文中一共展示了Response.status_string方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: upload

# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import status_string [as 别名]
def upload(request):
    if request.content_length/1000000 > 20:
        return error_response(400, 'Sorry, but the file must be under 20MB.')

    # Create photo object in database
    photo = Photo(datetime.today(), request.POST['file'].filename, request.client_addr, request.content_type, request.content_length)
    DBSession.add(photo)
    DBSession.flush()

    # Save uploaded file
    input_file = request.POST['file'].file
    input_file.seek(0)
    if not os.path.exists('data'):
        os.makedirs('data')
    if not os.path.exists('data/uploads'):
        os.makedirs('data/uploads')
    upload_path = os.path.join('data', 'uploads', str(photo.id))
    with open(upload_path, 'w') as f:
        shutil.copyfileobj(input_file, f)

    # Check the content type and rename as appropriate
    mime = magic.from_file(upload_path, mime=True)
    if mime not in ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/tiff', 'image/x-tiff']:
        resp = Response('Sorry, but we can only accept jpg, gif, or png files.')
        resp.status_code = 400
        resp.status_string = '400 Bad Request'
        return resp
    extension = {'image/jpeg': '.jpg', 'image/pjpeg': '.jpg',
                 'image/gif': '.gif', 'image/png': '.png',
                 'image/tiff': '.tiff', 'image/x-tiff': '.tiff'}[mime]
    os.rename(upload_path, upload_path + extension)
    photo.content_type = mime

    return Response('OK')
开发者ID:reberhardt7,项目名称:hcc-photo-upload,代码行数:36,代码来源:views.py

示例2: error_response

# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import status_string [as 别名]
def error_response(code, msg):
    status = {400: '400 Bad Request', 500: '500 Internal Server Error'}[code]
    resp = Response(msg)
    resp.status_code = code
    resp.status_string = status
    return resp
开发者ID:reberhardt7,项目名称:hcc-photo-upload,代码行数:8,代码来源:views.py


注:本文中的pyramid.response.Response.status_string方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。