Send file to remote php from local python in $_FILES -
preface: started learning python 1 month ago, please don't yell @ me :)
have full developed site written in php, functions need pages insert "stuff" in database. developing command line program insert same thing , send them php scripts in $_post array, have no idea on how send file come in $_files array.
here have far:
i send $_post values using urllib modules , works fine
import urllib.request import urllib.parse data=urllib.parse.urlencode({"hello":"yo!", "some":"alphabetaparkinglot"}) data=data.encode('utf_8') request=urllib.request.request("http://www.site.it/read_from_py.php") f=urllib.request.urlopen(request, data) out=f.read().decode('utf-8') if out[0]=="1": print("connection acquired!") else: print("no connection!") print("exit!") #then exit!
...i read filename local computer prompting choose file dialogbox...
from tkinter import tk tkinter.filedialog import askopenfilename tk().withdraw() filename=askopenfilename()
...but have no idea on filename. have looked around , have seen no way on how send $_files in php.
i have "dirty" solution: creating temporary html form file , make choose there, wondering if there cleaner solution.
thanks!
p.s. if wondering if have things need in php scripts, why need python program? well, correct, wouldn't need it's not me...
try this
import urllib,multipartposthandler,urllib2,cookielib cookies = cookielib.cookiejar() opener = urllib2.build_opener(urllib2.httpcookieprocessor(cookies),multipartposthandler.multipartposthandler) urllib2.install_opener(opener) login = urllib.urlencode(dict(admin_user='admin',admin_pass='****')) o=opener.open('http://some_domain_name.com/admin/index.php',login) print o.read() raw_params={"adtitle":"sample title", "area":"sample area", "addesc":"<p>sample post</p>", "pic[0]":open("indian_eye.jpg", "rb"), #file goes here "pic[1]":open("nature.jpg", "rb"), "subcatid":"1", "do":"post", } url="http://your.php?cityid=15&subcatid=1" opener.open(url, raw_params)
hope works!
Comments
Post a Comment