views:

41

answers:

0

Hi! I've this code:

1 upload_odl function

import os

import urllib2_files
import urllib2

user = 'patrick'
password = 'mypass'
url = 'http://localhost:8000/api/odl/'

password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(
    None, url, user, password
)

auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)

opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)

def upload_odl(filename, sky_code=46800):
    f = open(filename)

    params = {
        'installer_ac': str(sky_code),
        'pdf': {
            'fd': f,
            'filename': os.path.basename(filename),
        },
    }

    try:
        the_page = urllib2.urlopen(url, params)
        #response = urllib2.urlopen(req)
        #the_page = response.read()
    except urllib2.HTTPError, error:
        print error.read()

and this second snippet that search and uploads some documents

import os
import time

from rest import upload_odl

start_time = time.time()

ODL_DIRECTORY = '/Users/patrick/Documents/ODL'
UPLOAD_DIR = 'CARICA'

directories = os.listdir(ODL_DIRECTORY)

files = 0

try:
    for dir in directories:
        c = os.path.join(ODL_DIRECTORY, dir)
        if os.path.isdir(c):
            u = os.path.join(c, UPLOAD_DIR)
            if not os.path.exists(u):
                continue
            for x in os.listdir(u):
                upload_odl(os.path.join(u, x))
                #time.sleep(1)
                files += 1

    print 'Uploaded %d files in ' % len(to_upload),

    print time.time() - start_time, " seconds"
except IOError, e:
    #edit
    import sys, import traceback
    exc_type, exc_value, exc_traceback = sys.exc_info()
    traceback.print_exception(exc_type, exc_value, exc_traceback)

    print e, os.path.join(u, x)

when I run the code, I get this error, after 5 uploads:

patrick:odl_uploader patrick$ python __init__.py
[Errno 32] Broken pipe /Users/patrick/Documents/ODL/ARMINIO CHRISTIAN/CARICA/Di Salvo Agostino Antonio.pdf

EDIT:

Traceback output:

  File "__init__.py", line 25, in <module>
    upload_odl(os.path.join(u, x))
  File "/Users/patrick/Documents/django/installer/installer_crm/tools/odl_uploader/rest/__init__.py", line 32, in upload_odl
    the_page = urllib2.urlopen(url, params)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 124, in urlopen
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 383, in open
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 401, in _open
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 361, in _call_chain
  File "/Users/patrick/Documents/django/installer/installer_crm/tools/rest/urllib2_files.py", line 207, in http_open
  File "/Users/patrick/Documents/django/installer/installer_crm/tools/rest/urllib2_files.py", line 281, in do_open
  File "/Users/patrick/Documents/django/installer/installer_crm/tools/rest/urllib2_files.py", line 194, in send_data
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 719, in send
  File "<string>", line 1, in sendall
[Errno 32] Brok

en pipe /Users/patrick/Documents/ODL/ARMINIO CHRISTIAN/CARICA/Ferrante Carmine.pdf