ython模拟http表单上传文件的困惑

0 views
Skip to first unread message

isa rita

unread,
Jul 6, 2009, 1:44:16 AM7/6/09
to pyth...@googlegroups.com
总公司一个管理系统中需要手动上传文件
因为工作量比较大,决定用python实现批量功能

以下是手动上传时的httpheader信息
POST /func/save_upfile.jsp HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
Referer: https://192.168.1.10/func/save_upload.jsp
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7d9ab418901e6
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 192.168.1.10
Content-Length: 83924
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=E3EB68FFBCECDA19FDF3FB044401FAC0; cod=1050.1100.1200.1400.1500.1600

-----------------------------7d9ab418901e6
Content-Disposition: form-data; name="upfile"; filename="D:\report\0602.xls"
Content-Type: message/rfc822 
  
 file content……

因为之前模拟登录提交内容表单都是用的是 httplib.HTTPSConnection(site)来建立链接
然后conn.request("POST",url,params.headers)来提交数据

于是在模拟上传文件动作时出现了困惑
按照之前的做法把header信息都装在header这个dic里面
先是发现有两个"Content-Type"键,似乎没办法用dic来实现
然后是发现“Content-Length”不post的话服务器反馈错误
提交的话又不知如何确定这个长度

这种POST文件流的操作用哪个lib比较好呢?

shell909090

unread,
Jul 6, 2009, 2:02:58 AM7/6/09
to pyth...@googlegroups.com
isa rita 写道:
总公司一个管理系统中需要手动上传文件因为工作量比较大,决定用python实现批量功能

以下是手动上传时的httpheader信息

  
试试pycurl。

David Shen

unread,
Jul 6, 2009, 2:10:22 AM7/6/09
to pyth...@googlegroups.com
你file-content都能拿到,怎么拿不到长度呢?


2009/7/6 isa rita <isall...@gmail.com>:

--
Best Regards,
David Shen

http://twitter.com/davidshen84

isa rita

unread,
Jul 6, 2009, 2:17:30 AM7/6/09
to pyth...@googlegroups.com
能拿到,我直接file_content=open(file,'r')然后Len()出来的长度和 Httpheader传输数据比较总是差200多,而且差值还不固定,所以有些困惑,浏览器是如何计算这个Content-Length的。

2009/7/6 David Shen <david...@googlemail.com>

@@

unread,
Jul 6, 2009, 2:21:35 AM7/6/09
to pyth...@googlegroups.com

shell909090

unread,
Jul 6, 2009, 2:30:26 AM7/6/09
to pyth...@googlegroups.com
你第二个参数少个b

isa rita 写道:

isa rita

unread,
Jul 6, 2009, 10:15:52 AM7/6/09
to pyth...@googlegroups.com
谢谢几位的帮助,在各位指导下终于找到了问题所在
特别是找到了:

贴上来等,说不定其他鸟也会用得到。

2009/7/6 shell909090 <shell...@gmail.com>
Reply all
Reply to author
Forward
0 new messages