Ok so I have been searching for several days on how to write a simple python script that will HTTP post to my server. I am jsut looking for some tips on how to write this properly cause as of right now im having to upload my add and update files manually and it would make things easyer to be able to run a python script to do it for me. So any help would be greatly appriciated at this time.

Recommended Answers

All 5 Replies

Interesting,
So you wish to write a script that upload files via HTTP.

The key to this would probably be in 2 steps.

  1. Authenticating yourself with the server.
    You would probably need some way to authenticate yourself, Maybe a session id or other cookies will be returned to you. You can either login on your browser and then pass them as arguments to your script. Or let the script handle that.

  2. File transfer.
    In order to transfer files you'll need to make a multi-part HTTP request. you can use python's httplib or urllib to do this. I think we have a lot of helper libraries that you can find on the internet. (Just use your favourite search engine to look this up.)

This would be fine if you wish to upload files directly, however updating would be a completely different issue. And this is very specific to how your server handles update.

Before you consider doing the above, We would not require to do this if your server allows FTP. But if you're trying this out as an experiment. Feel free to add in more queries.

Sky what im wanting to to is looking like the file transfer. THe files im wanting to add to two seperate list are called add and update. I jsut need to know how to get python to login to my server then upload these two files to their designated list.

As I have mentioned earlier. If your server allows FTP, An FTP client would easily add and update files for you at your server.

If you wish to upload your files through your login panel, then the following is worth looking into.

  1. How is your server authenticating you. (Eg: username and password, that assigns you a particular sessionid cookie?)

  2. Once you are authenticated, you will then need to find out at which location can you add your files. ( Example file_upload.php ) And then determine what are the parameters that you need to pass for this to happen.

  3. Then send a HTTP request.

------------------

If you have already implemented a page on your server and simply need to call that with your 2 files.

Then you can skip all of the above and look into multi-part HTTP requests.

My apologies if I have confused you.

It authenticates me by username and password. I also have some code that im working with

import request, os, glob

os.chdir("/home/hatterx/Desktop/Bronto Files")
for FILE in glob.glob("FACTS_bronto_import_add_*")
        with open(FILE, 'rb') as f1:

url=self._resolve_url('website')
files = {'file': ('userfile', open(f1))}
data = {'account_id': account_id}
headers = {'content-type': 'multipart/form-data'}
res = requests.post(url, files=files, data=data, headers=headers)
return res.json

this is what i have so far but other than that im very conffused on what i need to do to get it to work right.

i figured out what i was doing wrong my whole code was wrong i had to have it look up the url then pass my paramaters through a data command.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.