简体   繁体   English

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

[英]trying to post file to php server using python and requests

I am trying to emulate this java code 我正在尝试模拟此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";

in python using requests, I think im missing something simple because I havent been able to get the right response from the server 在使用请求的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)

the response from the server that I keep getting is {"response":"error","comment":"missing at least one parameter"}' any help please? 我不断收到的来自服务器的响应是{“ response”:“错误”,“ comment”:“缺少至少一个参数”}'有什么帮助吗?

When you pass a dictionary to the data parameter of requests.post , it will be form-encoded. 当你传递一个字典的data参数requests.post ,这将是形式编码。 I'm not totally familiar with the Java code, but it looks like it is actually using an encoded String as the data of the request. 我对Java代码并不完全熟悉,但是看起来它实际上是在使用编码的String作为请求的数据。 In order to achieve the same thing with requests , pass a string as the data parameter, like so: 为了实现与requests相同的功能,请将字符串作为data参数传递,如下所示:

data = 'filedata=foo&filename=bar&systemid=&fooe&createNew=false'
r = requests.post(url, files=files, headers=headers, data=data)

See a similar example in the documentation here . 请参阅此处的文档中的类似示例。

You need to try changing the following things. 您需要尝试更改以下内容。

Firstly, data sends the given dictionary in the POST body. 首先, data在POST正文中发送给定的字典。 If that is genuinely where you want it, you need to remove the '=' elements from the dictionary: 如果这确实是您想要的位置,则需要从字典中删除'='元素:

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

Secondly, you're adding a pair of incorrect headers. 其次,您要添加一对错误的标题。 When you use Requests' file parameter, Requests prepares the body data (including your file) as multipart/form-data' . 使用Requests的file参数时,Requests将主体数据(包括文件)准备为multipart/form-data' You then override that Content-Type with your application/octet-stream , which is not the form of the body. 然后,您可以使用application/octet-stream覆盖该Content-Type,它不是主体的形式。 Additionally, you are claiming that your body data is compressed using deflate , which it is not. 此外,您声称您的身体数据是使用deflate ,而事实并非如此。

Try the following code: 尝试以下代码:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM