2

I am new to Python and it is first post. Want to upload a TXT file to server, (as of now, it is local host).Every time, I run the script, the local file uploaded and updated on server. I am using Requests module

import requests, time
url ='http://192.168.49.205/test/database/data.txt'  # where i want to write 
files = {'file':('data.txt','C:\Python27\data.txt','rb')}
#r = requests.post(url,files=files) # this works too

r= requests.post('http://192.168.49.205/test/database/data.txt',
data={'upload_type': 'standard',          'upload_to': '0'},files=files)

print r.status_code
print r.text

data.txt is not updated. Just seeing old data (I put some values when file created).I am not using any Forms in PHP or HTML. Is it possible to upload with method?

Believe,I got some clarity after posting the question. Now, there is PHP file on server side listening to client. Here is the "post.php". So, this will replace the text file on Client side. PHP file get the Name, Task, Value from client and post to "a.txt" on server (local)

 <?php    
 if(isset($_GET["Name"])){
 $name=$_GET["Name"];   
 }

 if(isset($_GET["Task"])){
 $task=$_GET["Task"];   
 }

if(isset($_GET["Value"])){
$value=$_GET["Value"];   
}

$f=fopen("a.txt","w") or exit("Unable to open file!");
fwrite($f,$name);
fwrite($f,"  ");
fwrite($f,$task);
fwrite($f,"  ");
fwrite($f,$value);
fclose($f);

?>`

So now Requests look like this

 import requests, time
 url = 'http://192.168.49.205/test/test.php' 
 post_data = {'Name':'job','Task':'008','Value':'8'}

 r= requests.post('http://192.168.49.205/test/post.php', data= post_data)


print r.status_code
print r.text

Stil the values is not reaching "a.txt". What i am missing? Please advise!

6
  • The error could be on the server side. Commented Nov 20, 2013 at 14:38
  • Does http://192.168.49.205/test/database/data.txt accept POST requests? What kind of service is listening there? Commented Nov 20, 2013 at 14:39
  • data.txt is plain text file. Do not think, it takes POST request or listening.After your question, i getting the idea. So, server file should be PHP file which accepts POST request. Please bear my ignorance. Commented Nov 20, 2013 at 14:45
  • url = 'httpbin.org/post' files = {'file': open('report.xls', 'rb')} r = requests.post(url, files=files). Here, on the URL what exactly post means? Is it continuously listening part on server? Commented Nov 20, 2013 at 14:53
  • There are other methods that may be supported but usually are not. PUT for example should do what you want. techforum4u.com/content.php/… Commented Nov 20, 2013 at 15:20

1 Answer 1

2

You're not reading the files content in your code. The requests documentation states:

files – (optional) Dictionary of ‘name’: file-like-objects (or {‘name’: (‘filename’, fileobj)}) for multipart encoding upload.

Your code suggests you want to use the second option. However:

files = {'file':('data.txt','C:\Python27\data.txt','rb')}

You're not creating the structure as the documentation indicates - you're passing a tuple of 3 strings instead of a tuple of (string, file_obj). You probably wanted to do this:

files = {'file':('data.txt',open('C:\Python27\data.txt','rb'))}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.