如何调试

2024-06-16 10:50:21 发布

您现在位置:Python中文网/ 问答频道 /正文

1上传功能

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()

第二个片段搜索并上传一些文档

^{pr2}$

当我运行代码时,在5次上传之后,我得到了这个错误:

patrick:odl_uploader patrick$ python __init__.py
[Errno 32] Broken pipe /Users/patrick/Documents/ODL/name.pdf

编辑:

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/name.pdf

Tags: inpylinelibraryinstallerpasswordopenurllib2