Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 966611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:11:05+00:00 2026-05-16T02:11:05+00:00

i made simple web server like below. import BaseHTTPServer, os, cgi import cgitb; cgitb.enable()

  • 0

i made simple web server like below.

import BaseHTTPServer, os, cgi
import cgitb; cgitb.enable()

html = """
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
File upload: <input type="file" name="upfile">
<input type="submit" value="upload">
</form>
</body>
</html>
"""
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("content-type", "text/html;charset=utf-8")
        self.end_headers()
        self.wfile.write(html)

    def do_POST(self):
        ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
        if ctype == 'multipart/form-data':
            query = cgi.parse_multipart(self.rfile, pdict)
            upfilecontent = query.get('upfile')
            if upfilecontent:
                # i don't know how to get the file name.. so i named it 'tmp.dat'
                fout = file(os.path.join('tmp', 'tmp.dat'), 'wb')
                fout.write (upfilecontent[0])
                fout.close()
        self.do_GET()

if __name__ == '__main__':
    server = BaseHTTPServer.HTTPServer(("127.0.0.1", 8080), Handler)
    print('web server on 8080..')
    server.serve_forever()

In the do_Post method of BaseHTTPRequestHandler, i got the uploaded file data successfully.

But i can’t figure out how to get the original name of the uploaded file.
self.rfile.name is just a ‘socket’
How can i get the uploaded file name?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:11:06+00:00Added an answer on May 16, 2026 at 2:11 am

    Pretty broken code you’re using there as a starting point (e.g. look at that global rootnode where name rootnode is used nowhere — clearly half-edited source, and badly at that).

    Anyway, what form are you using “client-side” for the POST? How does it set that upfile field?

    Why aren’t you using the normal FieldStorage approach, as documented in Python’s docs? That way, you could use the .file attribute of the appropriate field to get a file-like object to read, or its .value attribute to read it all in memory and get it as a string, plus the .filename attribute of the field to know the uploaded file’s name. More detailed, though concise, docs on FieldStorage, are here.

    Edit: now that the OP has edited the Q to clarify, I see the problem: BaseHTTPServer does not set the environment according to the CGI specs, so the cgi module isn’t very usable with it. Unfortunately the only simple approach to environment setting is to steal and hack a big piece of code from CGIHTTPServer.py (wasn’t intented for reuse, whence the need for, sigh, copy and paste coding), e.g….:

    def populenv(self):
            path = self.path
            dir, rest = '.', 'ciao'
    
            # find an explicit query string, if present.
            i = rest.rfind('?')
            if i >= 0:
                rest, query = rest[:i], rest[i+1:]
            else:
                query = ''
    
            # dissect the part after the directory name into a script name &
            # a possible additional path, to be stored in PATH_INFO.
            i = rest.find('/')
            if i >= 0:
                script, rest = rest[:i], rest[i:]
            else:
                script, rest = rest, ''
    
            # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
            # XXX Much of the following could be prepared ahead of time!
            env = {}
            env['SERVER_SOFTWARE'] = self.version_string()
            env['SERVER_NAME'] = self.server.server_name
            env['GATEWAY_INTERFACE'] = 'CGI/1.1'
            env['SERVER_PROTOCOL'] = self.protocol_version
            env['SERVER_PORT'] = str(self.server.server_port)
            env['REQUEST_METHOD'] = self.command
            uqrest = urllib.unquote(rest)
            env['PATH_INFO'] = uqrest
            env['SCRIPT_NAME'] = 'ciao'
            if query:
                env['QUERY_STRING'] = query
            host = self.address_string()
            if host != self.client_address[0]:
                env['REMOTE_HOST'] = host
            env['REMOTE_ADDR'] = self.client_address[0]
            authorization = self.headers.getheader("authorization")
            if authorization:
                authorization = authorization.split()
                if len(authorization) == 2:
                    import base64, binascii
                    env['AUTH_TYPE'] = authorization[0]
                    if authorization[0].lower() == "basic":
                        try:
                            authorization = base64.decodestring(authorization[1])
                        except binascii.Error:
                            pass
                        else:
                            authorization = authorization.split(':')
                            if len(authorization) == 2:
                                env['REMOTE_USER'] = authorization[0]
            # XXX REMOTE_IDENT
            if self.headers.typeheader is None:
                env['CONTENT_TYPE'] = self.headers.type
            else:
                env['CONTENT_TYPE'] = self.headers.typeheader
            length = self.headers.getheader('content-length')
            if length:
                env['CONTENT_LENGTH'] = length
            referer = self.headers.getheader('referer')
            if referer:
                env['HTTP_REFERER'] = referer
            accept = []
            for line in self.headers.getallmatchingheaders('accept'):
                if line[:1] in "\t\n\r ":
                    accept.append(line.strip())
                else:
                    accept = accept + line[7:].split(',')
            env['HTTP_ACCEPT'] = ','.join(accept)
            ua = self.headers.getheader('user-agent')
            if ua:
                env['HTTP_USER_AGENT'] = ua
            co = filter(None, self.headers.getheaders('cookie'))
            if co:
                env['HTTP_COOKIE'] = ', '.join(co)
            # XXX Other HTTP_* headers
            # Since we're setting the env in the parent, provide empty
            # values to override previously set values
            for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
                      'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
                env.setdefault(k, "")
            os.environ.update(env)
    

    This could be substantially simplified further, but not without spending some time and energy on that task:-(.

    With this populenv function at hand, we can recode:

    def do_POST(self):
        populen(self)
        form = cgi.FieldStorage(fp=self.rfile)
        upfilecontent = form['upfile'].value
        if upfilecontent:
            fout = open(os.path.join('tmp', form['upfile'].filename), 'wb')
            fout.write(upfilecontent)
            fout.close()
        self.do_GET()
    

    …and live happily ever after;-). (Of course, using any decent WSGI server, or even the demo one, would be much easier, but this exercise is instructive about CGI and its internals;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a simple web form in Google app engine where I have
I have made a simple web-page including a couple of static pages, a css
I've made a simple .NET 4 Web Application in VS2010, and added a reference
I made in my web a menu using generic_view - simple 'django.views.generic.list_detail.object_list' in urls.py
hi i've got question i've made simple form with attachment input and i want
I have made a simple server using TcpListener and it works great but now
I have made up a simple set of web services (trying to make a
I made a simple web crawler using PHP (and cURL). It parses rougly 60
I made simple email in wordpress but it would only send email to the
I have made simple encryption/decryption method in php that I'm trying to move to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.