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

  • SEARCH
  • Home
  • 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 8003271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:36:35+00:00 2026-06-04T16:36:35+00:00

After searching for how to POST large files using python, I came across this

  • 0

After searching for how to POST large files using python, I came across this and I wrote a program based on that, but when I run it I get the following error message:

  Traceback (most recent call last):
  File "Test3.py", line 187, in <module>
    main()
  File "Test3.py", line 184, in main
    do_upload(options, args)
  File "Test3.py", line 48, in do_upload
    response = urllib2.urlopen(request)
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 400, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error

and here is my code, to run the program I use --upload "path_to_file" [space] "filename". I’m new to python programming so most of this still confuses me.

def do_upload(options, args):
    url = 'http://127.0.0.1/test_server/upload'

    path = args[0]
   # print path
    filename = args[1]
    if not os.access(args[0], os.F_OK):
                print "Directory/file Doesn't exist"
                exit(1)

    os.chdir(path)
    f = open(filename, 'rb')
    mmapped_file_as_string = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)


    request = urllib2.Request(url, mmapped_file_as_string)
    contenttype = mimetypes.guess_type(filename)[0] 
    request.add_header(contenttype, 'application/octet-stream')
    response = urllib2.urlopen(request)


    #close everything
    mmapped_file_as_string.close()
    f.close()

UPDATE

I have changed the code from above and now I’m getting some socket error.

Updated code

def do_upload(options, args):

    host = 'http://localhost:80'
    selector = '/test_server/upload'
    url = 'http://localhost:80/test_server/upload'

    if len(args) == 2:
        print "len of args = 2"
        files = "File is " + str(args[1])
        print files
        path = args[0]
        print "Path is " + str(args[0])



    content_type, body = encode_multipart_formdata(files)
    h = httplib.HTTP(host)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body)))
    h.endheaders()
    h.send(body)
    errcode, errmsg, headers = h.getreply()
    return h.file.read()

    f = open(files, 'rb')
    mmapped_file_as_string = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
    request = urllib2.Request(url, mmapped_file_as_string)
    request.add_header('Content-Type', content_type)
    response = urllib2.urlopen(request)

    mmapped_file_as_string.close()
    f.close()


def encode_multipart_formdata(files):

    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = '\r\n'
    L = []

    for (filename) in files:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data;  filename="%s"' % (filename))
        L.append('Content-Type: %s' % get_content_type(filename))
        L.append('')
        #L.append(value)
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body

def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'

Error message

Traceback (most recent call last):
  File "Test3.py", line 208, in <module>
    main()
  File "Test3.py", line 205, in main
    do_upload(options, args)
  File "Test3.py", line 41, in do_upload
    h.endheaders()
  File "C:\Python27\lib\httplib.py", line 951, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 811, in _send_output
    self.send(msg)
  File "C:\Python27\lib\httplib.py", line 773, in send
    self.connect()
  File "C:\Python27\lib\httplib.py", line 754, in connect
    self.timeout, self.source_address)
  File "C:\Python27\lib\socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed
  • 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-06-04T16:36:36+00:00Added an answer on June 4, 2026 at 4:36 pm

    You are setting a non-existing header instead of the Content-Type header:

    request.add_header(contenttype, 'application/octet-stream')
    

    Change that to:

    request.add_header('Content-Type', contenttype)
    

    instead.

    Your biggest problem however is that you are not uploading a multipart POST, but rather just the file itself as the POST body, while your server is expecting only a multipart upload.

    Take a look at this SO answer for a very simple way to generate a proper multipart POST body: https://stackoverflow.com/a/681182/100297. Note that you’ll have to adjust your Content-Type header accordingly.

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

Sidebar

Related Questions

I came across this question after searching for a ODBC or JDBC. To my
My apologies if this is a duplicate post post but after some searching, I
After searching around for months on and off I finally decided to post this
There are a few posts about this, but after hours of searching I still
After searching through SO, and not finding anything useful, I decided to post this
after a bit of searching I was unable to find a helpful post so...
First attempt to use this cool site - after searching for 2 hours: So
I am really posting this out of desperation after searching around a lot for
UPDATED: 06.29.10 Here's the code I'm using so far. I'm really close after searching
I'm asking this question after searching the web for an answer for 2 days.

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.