在Python中真的可以POST文件吗?
我已经在这个问题上纠结了两天,还是没有解决。网上找到了一些解决方案,但每次尝试用POST发送文件时,依然出现“内部服务器错误”。我的想法是这样的:我在Python的命令行中打开一个文件,然后把它发送到我服务器上的一个Django函数,这个函数会读取并保存这个文件。我试过了urllib、urllib2(有和没有poster模块)、httplib和Multipart方法。以下是我尝试过的所有方法和结果:
#Variables
In [35]: url = "http://www.address"
In [36]: values = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
In [37]: dataurllib = urllib.urlencode(values)
In [39]: dataposterlib, dataheaders = multipart_encode(values)
In [40]: dataposterlib
Out[40]: <generator object yielder at 0x15a0410>
In [41]: headers
{'Content-Length': 491,
'Content-Type': 'multipart/form-data; boundary=13936c2ddba0441eab9c3f4133a4009e'}
In [43]: f = open("winter.jpg", "rb")
In [45]: filegen, fileheaders = multipart_encode({"file": f})
In [46]: filegen
Out[46]: <generator object yielder at 0xc5b3c0>
In [47]: fileheaders
Out[47]:
{'Content-Length': 39876,
'Content-Type': 'multipart/form-data; boundary=0aa7a1b68d714440be06e166080925ec'}
#Working:
1. urllib.urlopen("http://www.address", data=urllib.urlencode({"key": "value"}))
2. urllib.urlopen(url, data=urllib.urlencode({"key": "value"}))
3. urllib.urlopen(url, data=urlib.urlencode(values))
4. urllib.urlopen(url, urlib.urlencode(values))
5. urllib.urlopen(url, data=dataurllib)
6. urllib.urlopen(url, dataurllib)
7. urllib.urlopen(url, data=urllib.urlencode({"file": f})) : works, but doesnt send file
8. import cookielib
from utils.multipart import MultipartPostHandler
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), MultipartPostHandler)
params = { "username": "bob", "password": "riviera", "file": f }
opener.open(url, params)
#Not working:
4. u = urllib.urlopen(url, "string")
5. u = urllib.urlopen(url, data="string")
7. u = urllib2.urlopen(url, data=urllib.urlencode(values)) : Error 500 but sends data
6. u = urllib2.urlopen("http://www.fandrive.yum.pl/event/upload/", data=urllib.urlencode({"key": "value"})) : Error 500 but sends data
7. u = urllib2.urlopen(url, data=urllib.urlencode({"key": "value"})) : Error 500 but sends data
8. u = urllib2.urlopen(url, data=dataurllib) : Error 500 but sends data
9. u = urllib2.urlopen(url, dataurllib) : Error 500 but sends data
10. In [31]: req = urllib2.Request(url, f, fileheaders)
urllib2.urlopen(req) : Error 500
11. req = urllib2.Request(url, filegen, fileheaders)
urllib2.urlopen(req) : Error 500, very seldomly sends data
12. req = urllib2.Request(url, dataurllib)
urllib2.urlopen(req) : Error 500, sends data
我发现有其他人也遇到过同样的问题,所以我借用了他的Django函数(这个函数是基于这篇文章的):
def upload(request):
logging.debug("GO")
for key, file in request.FILES.items():
path = '/path/'+ file.name
logging.debug(path)
dest = open(path.encode('utf-8'), 'wb+')
logging.debug(dest)
if file.multiple_chunks:
loggin.debug("big")
for c in file.chunks():
dest.write(c)
else:
logging.dobug("small")
dest.write(file.read())
dest.close()
destination = path + 'test.txt'
logging.debug(destination)
file = open(destination, "a")
file.write("file \n")
file.close
return 0
我注意到即使我的函数在运行,发送的文件也没有被保存,而且在destination...
之后的代码也被跳过了。就好像在到达这个地方之前就超时了。Wireshark显示POST请求已经发送,但服务器总是返回500错误。这是怎么回事呢?我尝试了几种组合,使用我的两个服务器以及从本地计算机发送,但还是没有结果。我到底漏掉了什么?我用的代码和网上其他人一样。即使是内置的urllib2模块也总是出错,尽管urllib可以正常工作。
编辑
我把服务器换成了另一个,终于得到了一个可靠的错误日志(接收方):
[Sat Oct 16 20:40:46 2010] [error] [client IP] mod_python (pid=24773, interpreter='host', phase='PythonHandler', handler='django.core.handlers.modpython'): Application error
[Sat Oct 16 20:40:46 2010] [error] [client IP] ServerName: 'xxx'
[Sat Oct 16 20:40:46 2010] [error] [client IP] DocumentRoot: 'path'
[Sat Oct 16 20:40:46 2010] [error] [client IP] URI: '/error/internalServerError.html'
[Sat Oct 16 20:40:46 2010] [error] [client IP] Location: None
[Sat Oct 16 20:40:46 2010] [error] [client IP] Directory: None
[Sat Oct 16 20:40:46 2010] [error] [client IP] Filename: 'path/error/internalServerError.html'
[Sat Oct 16 20:40:46 2010] [error] [client IP] PathInfo: ''
[Sat Oct 16 20:40:46 2010] [error] [client IP] Traceback (most recent call last):
[Sat Oct 16 20:40:46 2010] [error] [client IP] File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1537, in HandlerDispatch\n default=default_handler, arg=req, silent=hlist.silent)
[Sat Oct 16 20:40:46 2010] [error] [client IP] File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1229, in _process_target\n result = _execute_target(config, req, object, arg)
[Sat Oct 16 20:40:46 2010] [error] [client IP] File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1128, in _execute_target\n result = object(arg)
[Sat Oct 16 20:40:46 2010] [error] [client IP] File "/usr/lib/python2.5/site-packages/django/core/handlers/modpython.py", line 228, in handler\n return ModPythonHandler()(req)
[Sat Oct 16 20:40:46 2010] [error] [client IP] File "/usr/lib/python2.5/site-packages/django/core/handlers/modpython.py", line 220, in __call__\n req.write(chunk)
[Sat Oct 16 20:40:46 2010] [error] [client IP] IOError: Write failed, client closed connection.
[Sat Oct 16 20:40:46 2010] [error] [client IP] python_handler: Dispatch() returned non-integer.
1 个回答
重要更新:
非常抱歉,我给你提供了错误的代码。我使用的有效代码是基于这个链接的内容:http://code.activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/,所以我在这里不会重复它,也不想说这是我的代码。
因为你的评论,我阅读了RFC1867,意识到我给你的是错误的代码(我也在用这个,它很方便,如果你能控制两边的话)。
你的错误日志显示了IOError: Write failed, client closed connection.
,我猜这意味着服务器试图把一些数据(可能是文件?)写到一个不应该写的地方,或者它没有权限在那个地方写。你应该检查一下这个地方,并查看服务器运行的用户是否有写权限。