2

I'd like to upload files to a server using python. That means in this case that when I write http://example.com/files/ in my browser I'd like to see de.txt there in the list of files. How should I change this script to be working? When I run it, python shell writes out nothing, only the >>>.

Thanks in advance!

import requests

url = 'http://example.com/files/'
user, password = 'ex', 'ample'

files = {'upload_file': open(r'C:\Users\example\Desktop\code\de.txt','rb')}

r = requests.post(url,  auth=(user, password), files=files)
Marci
  • 427
  • 2
  • 9
  • 20
  • possible duplicate of https://stackoverflow.com/questions/22567306/python-requests-file-upload – Amrit Sep 12 '17 at 12:48

1 Answers1

0

Writing a file across network

import requests
from requests.auth import HTTPBasicAuth

url = 'http://example.com/files/'
user, password = 'ex', 'ample'

files = {'file': ('de.txt', open('de.txt', 'rb'), 'multipart/form-data', {'Expires': '0'})}

r = requests.post(url,  auth=HTTPBasicAuth(user, password), files=files)

For reference use Python Request documentation

Anurag Misra
  • 1,516
  • 18
  • 24