python - Urllib/JSON request from text file -
i trying send data text file server looking match sent data in order matched data returned me store in existing text file. if send list of names server within script, fine. want repeat request , insert text file names matched , returned. here text far:
import json import urllib2 values = 'e:\names.txt' url = 'https://myurl.com/get?name=values&key=##########' response = json.load(urllib2.urlopen(url)) open('e:\data.txt', 'w') outfile: json.dump(response, outfile, sort_keys = true, indent = 4,ensure_ascii=false);
this code send 1 line file showing nothing has matched. assuming looking @ values name instead of data in values text file.
update trial 1: updated code per suggested below include urllib.urlencode suggestion. here updated code:
import json import urllib import urllib2 file = 'e:\names.txt' url = 'https://myurl.com/get' values = {'name' : file, 'key' : '##########'} data = urllib.urlencode(values) req = urllib2.request(url, data) response = json.load(urllib2.urlopen(req)) open('e:\data.txt', 'w') outfile: json.dump(response, outfile, sort_keys = true, indent = 4,ensure_ascii=false);
fixed traceback errors editing url. passing "e:\names.txt" name in json request. seems issue trying send data in names.txt file tuple 'names' properly. thoughts?
make sure when sending parameters server, they're encoded -- see urllib.urlencode()
Comments
Post a Comment