php客户端与服务器对接文件,使用python客户端和recei将文件上载到php服务器

Apache、Php、Python

这个问题在这个网站上已经被问过很多次了,但是作为python和php的新手,我很难找到一个正确的方法。在

目前

我的客户端部分看起来像(压缩文件并发送)1 #!/usr/bin/python

2

3 import os

4 import zipfile

5 import sys

6 import hashlib

7 from poster.encode import multipart_encode

8 from poster.streaminghttp import register_openers

9 import urllib2

10

11 def zip(src, dst):

12 zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)

13 abs_src = os.path.abspath(src)

14 for dirname, subdirs, files in os.walk(src):

15 for filename in files:

16 absname = os.path.abspath(os.path.join(dirname, filename))

17 arcname = absname[len(abs_src) + 1:]

18 print 'zipping %s as %s' % (os.path.join(dirname, filename), arcname)

19 zf.write(absname, arcname)

20 zf.close()

21

22 # zip the file using source to the destination.. can do some error checks here

23 zip(sys.argv[1], sys.argv[2])

24

25 # create md5

26 md5 = hashlib.md5(open(sys.argv[2]+".zip", 'rb').read()).hexdigest()

27

28 # Register the streaming http handlers with urllib2

29 register_openers()

30

31 filename=sys.argv[2]+".zip"

32

33 # headers contains the necessary Content-Type and Content-Length

34 # datagen is a generator object that yields the encoded parameters

35 datagen, headers = multipart_encode({

36 'type' : 'zip',

37 'name' : "hello.zip",

38 'file' : open(filename)

39 })

40

41 # make a call

42 request = urllib2.Request("http://localhost/upload.php", datagen, headers)

43

44

45 # Actually do the request, and get the response

46 print urllib2.urlopen(request).read()

服务器端看起来像

^{pr2}$

当我运行python文件时,我得到这个输出

上载:

类型:

大小:0 Kb

存储在:

这种情况特别适用于较大的文件。在

问题的第二部分,当我发送小文件时,我看到了这个Upload: upload.php
Type: application/x-httpd-php
Size: 0.4775390625 Kb
Stored in: /private/var/tmp/phpHapPaO

Beautiful-iMac:~ agauravdeep$ open /private/var/tmp/

Beautiful-iMac:~ agauravdeep$ cd /private/var/tmp/phpHapPaO

-bash: cd: /private/var/tmp/phpHapPaO: No such file or directory

Beautiful-iMac:~ agauravdeep$ vi /private/var/tmp/phpHapPaO

You have new mail in /var/mail/agauravdeep

但那里什么也没有。正如在下面的评论中提到的,我试图更新php.ini.default但是即使重新启动了phpinfo,我也没有得到任何更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值