免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5186 | 回复: 1
打印 上一主题 下一主题

用python如何实现上传图片到HTTP服务器? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-23 13:27 |只看该作者 |倒序浏览
抓包如下:

  1. POST /cgi-bin/fileupload?job=add HTTP/1.1\r\n
  2. Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
  3. application/x-shockwave-flash, application/msword, application/vnd.ms-
  4. excel, application/vnd.ms-powerpoint, */*\r\n
  5. Referer: [url]http://bbs.stu.edu.cn/cgi-bin/fileupload[/url]\r\n
  6. Accept-Language: zh-cn\r\n
  7. Content-Type: multipart/form-data;
  8. boundary=---------------------------7d91f129901cc\r\n
  9. Accept-Encoding: gzip, deflate\r\n
  10. User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
  11. SV1; .NET CLR 2.0.50727)\r\n
  12. Host: bbs.stu.edu.cn\r\n
  13. Content-Length: 259560
  14. Connection: Keep-Alive\r\n
  15. Cache-Control: no-cache\r\n
  16. Cookie: utmpnum=1019; utmpkey=33895263; utmpuserid=yorks\r\n
  17. \r\n

  18. First boundary: -----------------------------7d91f129901cc\r\n
  19. Content-Disposition: form-data; name="upload_file"; filename="V:\yorks
  20. \Documents\kernel_dev_persons1.jpg"\r\n
  21. Content-Type: image/pjpeg\r\n\r\n
  22. Last boundary: \r\n-----------------------------7d91f129901cc--\r\n
复制代码

我用urllib2发送的时候通过抓包,发现和通过浏览器的不一样。不知道哪里出错了,麻烦各位帮忙看看。
    fileObj=open(picturePath,"rb")
    file=fileObj.read()
    fileObj.close()
    #BOUNDARY = mimetools.choose_boundary()
    #print BOUNDARY
    BOUNDAY = "---------------------------7d91f129901cc"
    #print BOUNDAY
    CRLF = '\r\n'
    lines = []
    lines.append('--'+BOUNDAY)
    lines.append('Content-Disposition: form-data; name="upload_file"; filename="%s"' % picturePath)
    lines.append('Content-Type: image/pjpeg'+CRLF)
    lines.append('')
    lines.append(file)
    lines.append(BOUNDAY + '--')
    lines.append('')
    body = CRLF.join(lines)
    #print body
    
    uploadHeaders = {
                      'Accept':'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, applic
ation/vnd.ms-excel, application/vnd.ms-powerpoint, */*'
,
                      'Referer':'http://bbs.stu.edu.cn/cgi-bin/fileupload',
                      'Accept-Language':'zh-cn',
                      'Content-Type':'multipart/form-data; boundary=%s' % BOUNDAY,
                      #'Accept-Encoding':'gzip, deflate',
                      'User-Agent':'Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5',
                      'Host': 'bbs.stu.edu.cn',
                      'Content-Length':'%s' % len(body),
                      'Connection':'Keep-Alive',
                      'Cache-Control':'no-cache',
                      'Cookie':'utmpnum=%s; utmpkey=%s; utmpuserid=%s' % (utmpnum, utmpkey, utmpuserid),
                   }
    print uploadHeaders
    uploadURL = "http://bbs.stu.edu.cn/cgi-bin/fileupload?job=add"
    request = urllib2.Request(url=uploadURL,headers=uploadHeaders, data=body)
    conn = urllib2.urlopen(request)
    header = str(conn.info())
    res = conn.read()

论坛徽章:
0
2 [报告]
发表于 2009-12-23 23:43 |只看该作者
已经搞定。。原来服务器的cgi程序是通过\来判断文件名的。。。所以filename= 这里一定包含\+文件名
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP