コード例 #1
0
ファイル: test_file_upload.py プロジェクト: ailling/webtest
def upload_binary_app(environ, start_response):
    req = Request(environ)
    status = "200 OK"
    if req.method == "GET":
        body = to_bytes("""
<html>
    <head><title>form page</title></head>
    <body>
        <form method="POST" id="binary_upload_form"
              enctype="multipart/form-data">
            <input name="binary-file-field" type="file" />
            <input name="button" type="submit" value="binary" />
        </form>
    </body>
</html>
""")
    else:
        uploaded_files = req.POST.getall("binary-file-field")
        data = [str(n) for n in struct.unpack('255h', uploaded_files[0].value)]
        body = to_bytes("""
<html>
    <head><title>display page</title></head>
    <body>
        %s
    </body>
</html>
""" % join_bytes(',', data))
    headers = [
        ('Content-Type', 'text/html; charset=utf-8'),
        ('Content-Length', str(len(body)))]
    start_response(status, headers)
    return [body]
コード例 #2
0
def upload_binary_app(environ, start_response):
    req = Request(environ)
    status = "200 OK"
    if req.method == "GET":
        body = to_bytes("""
<html>
    <head><title>form page</title></head>
    <body>
        <form method="POST" id="binary_upload_form"
              enctype="multipart/form-data">
            <input name="binary-file-field" type="file" />
            <input name="button" type="submit" value="binary" />
        </form>
    </body>
</html>
""")
    else:
        uploaded_files = req.POST.getall("binary-file-field")
        data = [str(n) for n in struct.unpack('255h', uploaded_files[0].value)]
        body = to_bytes("""
<html>
    <head><title>display page</title></head>
    <body>
        %s
    </body>
</html>
""" % join_bytes(',', data))
    headers = [('Content-Type', 'text/html; charset=utf-8'),
               ('Content-Length', str(len(body)))]
    start_response(status, headers)
    return [body]
コード例 #3
0
ファイル: test_file_upload.py プロジェクト: ailling/webtest
def multiple_upload_file_app(environ, start_response):
    req = Request(environ)
    status = "200 OK"
    if req.method == "GET":
        body = to_bytes(
"""
<html>
    <head><title>form page</title></head>
    <body>
        <form method="POST" id="file_upload_form"
              enctype="multipart/form-data">
            <input name="file-field-1" type="file" />
            <input name="file-field-2" type="file" />
            <input name="button" type="submit" value="single">
        </form>
    </body>
</html>
""")
    else:
        uploaded_file_1 = req.POST.get("file-field-1")
        uploaded_file_2 = req.POST.get("file-field-2")
        uploaded_files = [uploaded_file_1, uploaded_file_2]

        body_head = to_bytes(
"""
<html>
    <head><title>display page</title></head>
    <body>
""")

        file_parts = []
        for uploaded_file in uploaded_files:
            print (to_bytes(uploaded_file.filename), type(uploaded_file.value))
            file_parts.append(
"""
        <p>You selected '%(filename)s'</p>
        <p>with contents: '%(value)s'</p>
""" % dict(filename=to_string(uploaded_file.filename),
           value=to_string(uploaded_file.value)))

        body_foot = to_bytes(
"""    </body>
</html>
""")
        body = body_head + join_bytes("", file_parts) + body_foot
    headers = [
        ('Content-Type', 'text/html; charset=utf-8'),
        ('Content-Length', str(len(body)))]
    start_response(status, headers)
    return [body]
コード例 #4
0
    def submit_app(environ, start_response):
        req = Request(environ)
        status = "200 OK"
        if req.method == "GET":
            body = to_bytes("""
<html>
  <head><title>form page</title></head>
  <body>
    <form
        id="%s"
        action=""
        method="POST"
        enctype="multipart/form-data"
        accept-charset="utf-8">

      %s
    </form>
  </body>
</html>
""" % (form_id, form_fields_text))
        else:
            body_head = to_bytes("""
<html>
    <head><title>display page</title></head>
    <body>
""")

            body_parts = []
            for (name, value) in req.POST.items():
                if hasattr(value, 'filename'):
                    body_parts.append(
                        "%s:%s:%s\n" %
                        (to_string(name), to_string(
                            value.filename), to_string(value.value)))
                else:
                    body_parts.append("%s:%s\n" %
                                      (to_string(name), to_string(value)))

            body_foot = to_bytes("""    </body>
    </html>
    """)
            body = body_head + join_bytes("", body_parts) + body_foot
        headers = [('Content-Type', 'text/html; charset=utf-8'),
                   ('Content-Length', str(len(body)))]
        start_response(status, headers)
        assert (isinstance(body, binary_type))
        return [body]
コード例 #5
0
def multiple_upload_file_app(environ, start_response):
    req = Request(environ)
    status = "200 OK"
    if req.method == "GET":
        body = to_bytes("""
<html>
    <head><title>form page</title></head>
    <body>
        <form method="POST" id="file_upload_form"
              enctype="multipart/form-data">
            <input name="file-field-1" type="file" />
            <input name="file-field-2" type="file" />
            <input name="button" type="submit" value="single">
        </form>
    </body>
</html>
""")
    else:
        uploaded_file_1 = req.POST.get("file-field-1")
        uploaded_file_2 = req.POST.get("file-field-2")
        uploaded_files = [uploaded_file_1, uploaded_file_2]

        body_head = to_bytes("""
<html>
    <head><title>display page</title></head>
    <body>
""")

        file_parts = []
        for uploaded_file in uploaded_files:
            print(to_bytes(uploaded_file.filename), type(uploaded_file.value))
            file_parts.append("""
        <p>You selected '%(filename)s'</p>
        <p>with contents: '%(value)s'</p>
""" % dict(filename=to_string(uploaded_file.filename),
            value=to_string(uploaded_file.value)))

        body_foot = to_bytes("""    </body>
</html>
""")
        body = body_head + join_bytes("", file_parts) + body_foot
    headers = [('Content-Type', 'text/html; charset=utf-8'),
               ('Content-Length', str(len(body)))]
    start_response(status, headers)
    return [body]