0

Apache, Php, Python

This question has been asked on multiple occasions on this website but being very new to both python and php, it was hard for me to figure out a right way to do it.

Currently portion of my client side looks like (zips a file and send it)

  1 #!/usr/bin/python
  2 
  3 import os
  4 import zipfile
  5 import sys
  6 import hashlib
  7 from poster.encode import multipart_encode
  8 from poster.streaminghttp import register_openers
  9 import urllib2
 10 
 11 def zip(src, dst):
 12     zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
 13     abs_src = os.path.abspath(src)
 14     for dirname, subdirs, files in os.walk(src):
 15         for filename in files:
 16             absname = os.path.abspath(os.path.join(dirname, filename))
 17             arcname = absname[len(abs_src) + 1:]
 18             print 'zipping %s as %s' % (os.path.join(dirname, filename), arcname)
 19             zf.write(absname, arcname)
 20     zf.close()
 21 
 22 # zip the file using source to the destination.. can do some error checks here
 23 zip(sys.argv[1], sys.argv[2])
 24 
 25 # create md5
 26 md5 = hashlib.md5(open(sys.argv[2]+".zip", 'rb').read()).hexdigest()
 27 
 28 # Register the streaming http handlers with urllib2
 29 register_openers()
 30 
 31 filename=sys.argv[2]+".zip"
 32 
 33 # headers contains the necessary Content-Type and Content-Length
 34 # datagen is a generator object that yields the encoded parameters
 35 datagen, headers = multipart_encode({
 36     'type'      :       'zip',
 37     'name'      :       "hello.zip",
 38     'file'      :       open(filename)
 39 })
 40 
 41 # make a call
 42 request = urllib2.Request("http://localhost/upload.php", datagen, headers)
 43 
 44 
 45 # Actually do the request, and get the response
 46 print urllib2.urlopen(request).read()

Server side looks like

  1 <?php
  2 if ($_FILES["file"]["error"] > 0) {
  3         echo "Error: " . $_FILES["file"]["error"] . "<br />";
  4     } else {
  5         echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  6         echo "Type: " . $_FILES["file"]["type"] . "<br />";
  7         echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  8         echo "Stored in: " . $_FILES["file"]["tmp_name"];
  9     }
 15 
 16 
 17 ?>

When i run my python file, i get this output

Upload:
Type:
Size: 0 Kb
Stored in:

This happens specially for bigger files.

2nd part of the question, when i am sending the small file i see this

Upload: upload.php<br />Type: application/x-httpd-php<br />Size: 0.4775390625 Kb<br />Stored in: /private/var/tmp/phpHapPaO
Beautiful-iMac:~ agauravdeep$ open  /private/var/tmp/
Beautiful-iMac:~ agauravdeep$ cd /private/var/tmp/phpHapPaO
-bash: cd: /private/var/tmp/phpHapPaO: No such file or directory
Beautiful-iMac:~ agauravdeep$ vi /private/var/tmp/phpHapPaO
You have new mail in /var/mail/agauravdeep

But There is nothing there. As mentioned in comments below, i have tried to update php.ini.default but i didn't get any change updated with phpinfo even with restart

bruceparker
  • 1,235
  • 1
  • 17
  • 33
  • These work exactly as written for me (plus imports on the python script). I get outputs like: `Upload: .zshrc
    Type: text/plain
    Size: 0.9697265625 Kb
    Stored in: /tmp/phplocfny`
    – kalhartt Nov 14 '14 at 07:05
  • you are right.. i sent a small file and it worked but i am sending a huge file and that doesn't work.. – bruceparker Nov 14 '14 at 07:16
  • What size files are you sending? It is working for me with a ~900Mb file. – kalhartt Nov 14 '14 at 07:18
  • Also is your php.ini set to allow large files? I dont think the dev server `php -S` has a limitation on this. – kalhartt Nov 14 '14 at 07:22
  • How do you do that? Allow large files – bruceparker Nov 14 '14 at 07:23
  • [Try this](http://stackoverflow.com/questions/16102809/how-to-upload-large-files-above-500mb-in-php) – kalhartt Nov 14 '14 at 07:27
  • Thanks. I tried modifying .ini with post_max_size = 10000M, upload_max_filesize = 10000M, and max_execution_time = 300 but didn't work. Chunking seems to be a complex solution and last resort if we can't come up with an easier solution. Since you are able to send 900 MB file i am just wondering what could i be doing wrong... FYI i copied the entire code now – bruceparker Nov 14 '14 at 07:36

2 Answers2

0

this is my code for uploading file

import poster, urllib2, gzip, StringIO, sys

def getUrllib2(upload = False, redirect = False):
    if upload:
        handlers = poster.streaminghttp.get_handlers()
    else:
        handlers = []
    err = HTTPError() 
    handlers.append(err)  
    handlers.append(XmlParseTest.getCookie())

    try:
        opener = urllib2.build_opener(*handlers)
    except Exception, e:
        print err.getErrorMsg()
        raise e
    return opener,err

def sendMultipartPost(url, params, files):
    #params like this: {'id': '20010', 'type':'zip'}
    #files like thie : {'uploadfile':'/tmp/testfile.zip'}
    posterParams = []
    for key in params:
        value = params[key]
        try:                
            posterParams.append(poster.encode.MultipartParam(key, value))
        except Exception, e:
            print e, key, value    
            raise e

    for key in files:
        value = files[key]
        try:
            value = value.encode(sys.getfilesystemencoding())
            posterParams.append(poster.encode.MultipartParam.from_file(key, value))
        except Exception, e:
            print e, key, value    
            raise e

    try:    
        datagen, headers = poster.encode.multipart_encode(posterParams)
    except Exception, e:
        print e, key, value    
        raise e

    if headers is None:
        headers = {}            

    try: 
        request = urllib2.Request(url, datagen, headers)   
        request.add_header('Accept-encoding', 'gzip')   
        request.add_header("Accept", "*/*")     
#             print request   
#             print request.get_data()
        opener,err = getUrllib2(True, False)
        response = opener.open(request)
    except Exception, e:
        print e, url, files
        print err.getErrorMsg()
        raise e
    data = response.read()
    '''if response is too large, use this
    data = response.read(16*1024)
    length = len(data)
    _data = None
    while length:
        if _data: data += _data
        _data = response.read(16*1024)
        length = len(_data)
    '''    
    if 'gzip' == response.headers.get('content-encoding', ''):
        compressedstream = StringIO.StringIO(data)
        gzipper = gzip.GzipFile(fileobj=compressedstream)
        data =gzipper.read()            

    return data
NeoWu
  • 241
  • 2
  • 4
0

Php and Apache were referring to /etc/php.ini.default and there was no php.ini. I ended up creating php.ini with exact same configuration. After restart, i see the changes.

bruceparker
  • 1,235
  • 1
  • 17
  • 33