试图使用python和请求将文件发布到php服务器

时间:2013-07-02 17:53:40

标签: php python python-requests

我试图模仿这个java代码

            public static void uploadFile(String filename, String systemID){
            try{
                    String createNew = "false";

                    //check for backup files to know if we should make a new file on the server
                    File f = new File(filename + ".1");
                    if(f.exists()){
                            createNew = "true";
                            f.delete();
                    }

                    HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL(uploadURL).openConnection();
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setRequestMethod("POST");
            httpUrlConnection.setRequestProperty("Content-encoding", "deflate");
            httpUrlConnection.setRequestProperty("Content-type", "application/octet-stream");
            java.io.OutputStream os = httpUrlConnection.getOutputStream();
            Thread.sleep(1000);

            String fileData = IOUtils.toString(new FileReader(filename));

            String request = "filedata=" + fileData + "&filename=" + filename + "&systemid=" + systemID + "&createNew=" + createNew;

            DeflaterOutputStream deflate = new DeflaterOutputStream(os);
            deflate.write(request.getBytes());
            deflate.flush();
            deflate.close();

            os.close();
            BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));

            String s = null;
            while ((s = in.readLine()) != null) {
                System.out.println(s);
            }
            in.close();
            //fis.close();
            }catch(Exception e){
                    e.printStackTrace();
            }
            static String uploadURL = "http://vp-creations.com/utilities/fileupload.php";
在使用请求的python中,我认为我错过了一些简单的东西,因为我无法从服务器获得正确的响应

import requests
url = 'http://vp-creations.com/utilities/fileupload.php'
files = {'file': open('C:\\etc\\guitartab.txt', 'rb')}
headers = {'Content-encoding': 'deflate', 'Content-type': 'application/octet-stream'}

payload = {'filedata=': 'foo', 'filename=': 'bar', 'systemid=' : 'fooe', 'createNew=' : 'false'}

r = requests.post(url, files=files, headers=headers, data=payload)

我一直得到的服务器的响应是 {“response”:“错误”,“评论”:“缺少至少一个参数”}' 有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

将字典传递给data的{​​{1}}参数时,它将进行表单编码。我并不完全熟悉Java代码,但看起来它实际上是使用编码的requests.post作为请求的数据。为了与String实现相同的目标,请将字符串作为requests参数传递,如下所示:

data

请参阅文档here中的类似示例。

答案 1 :(得分:0)

您需要尝试更改以下内容。

首先,data在POST正文中发送给定的字典。如果这真的是你想要的地方,你需要从字典中删除'='元素:

payload = {'filedata': 'foo', 'filename': 'bar', 'systemid' : 'fooe', 'createNew' : 'false'}

其次,您正在添加一对不正确的标头。当您使用请求'file参数时,请求会将正文数据(包括您的文件)准备为multipart/form-data'。然后,您使用application/octet-stream覆盖该内容类型,而不是正文的形式。此外,您声称您的正文数据是使用deflate压缩的,而不是。{/ p>

请尝试以下代码:

import requests
url = 'http://vp-creations.com/utilities/fileupload.php'
files = {'file': open('C:\\etc\\guitartab.txt', 'rb')}

payload = {'filedata': 'foo', 'filename': 'bar', 'systemid' : 'fooe', 'createNew' : 'false'}

r = requests.post(url, files=files, data=payload)