def request(self, method, path, data=None, file=None): path = path.encode("ascii", "ignore") if (method == "GET" and data != None): path = path + "?" + urllib.urlencode(data) if (method != "GET" and method != "POST"): path = path + "?_method=" + method request = urllib2.Request(self.__application.config.server_api_url + "/v1" + path) base64string = base64.encodestring( '%s:%s' % (self.__application.token, self.__application.private_key)).replace('\n', '') request.add_header("Authorization", "Basic %s" % base64string) if (method == "GET"): result = urllib2.urlopen(request) else: if (data == None): data = {} if (file == None): result = urllib2.urlopen(request, urllib.urlencode(data)) else: form = MultiPartForm() for k, v in data.iteritems(): form.add_field(k, v) form.add_file('file', ntpath.basename(file), fileHandle=open(file)) body = str(form) request.add_header('Content-type', form.get_content_type()) request.add_header('Content-length', len(body)) result = urllib2.urlopen(request, body) return result.read()
def request(self, method, path, data = None, file = None): path = path.encode("ascii", "ignore") if (method == "GET" and data != None): path = path + "?" + urllib.urlencode(data) if (method != "GET" and method != "POST"): path = path + "?_method=" + method request = urllib2.Request(self.__application.config.server_api_url + "/v1" + path) base64string = base64.encodestring('%s:%s' % (self.__application.token, self.__application.private_key)).replace('\n', '') request.add_header("Authorization", "Basic %s" % base64string) if (method == "GET"): result = urllib2.urlopen(request) else: if (data == None): data = {} if (file == None): result = urllib2.urlopen(request, urllib.urlencode(data)) else: form = MultiPartForm() for k, v in data.iteritems(): form.add_field(k, v) form.add_file('file', ntpath.basename(file), fileHandle=open(file)) body = str(form) request.add_header('Content-type', form.get_content_type()) request.add_header('Content-length', len(body)) result = urllib2.urlopen(request, body) return result.read()
def upload(picPath): # Create the form form = MultiPartForm() # Add the image and required fields mimeType = mimetypes.guess_type(picPath)[0] form.add_file("encoded_image", picPath, StringIO(""), mimeType) form.add_field("image_content", "") # Build the request request = urllib2.Request("http://www.google.fr/searchbyimage/upload") body = str(form) request.add_header("Content-type", "%s;boundary=----WebKitFormBoundaryB6DC4larUvuT5gBS" % (form.get_content_type())) request.add_header("Content-length", len(body)) request.add_header( "User-Agent", "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36", ) request.add_data(body) print print "REQUEST:" print request.get_data() print print "SERVER RESPONSE:" # print urllib2.urlopen(request).read() print urllib2.urlopen(request).info()
def upload(picPath): # Create the form form = MultiPartForm() # Add the image and required fields mimeType = mimetypes.guess_type(picPath)[0] form.add_file('encoded_image', picPath, StringIO(''), mimeType) form.add_field('image_content', '') # Build the request request = urllib2.Request('http://www.google.fr/searchbyimage/upload') body = str(form) request.add_header( 'Content-type', '%s;boundary=----WebKitFormBoundaryB6DC4larUvuT5gBS' % (form.get_content_type())) request.add_header('Content-length', len(body)) request.add_header( 'User-Agent', 'User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36' ) request.add_data(body) print print 'REQUEST:' print request.get_data() print print 'SERVER RESPONSE:' #print urllib2.urlopen(request).read() print urllib2.urlopen(request).info()
headers = { 'Content-Type' :'application/x-www-form-urlencoded'} form = MultiPartForm() if file_to_publish.count("building"): form.add_field('id', 'interna_broken_link_%s' % timestamp) form.add_field('title', 'Internal Broken Link Report %s-%s-%s' % (now.year, now.month, now.day) ) else: form.add_field('id', 'total_broken_link_%s' % timestamp) form.add_field('title', 'Total Broken Link Report %s-%s-%s' % (now.year, now.month, now.day) ) form.add_field('form_submit', 'Save') form.add_field('form.submitted','1') # Add the file f = open(file_to_publish,'r') form.add_file('file_file', 'checklink_output.html', fileHandle = f ) f.close() # Build the request req = urllib2.Request(url) req.add_header('User-agent', 'PyMOTW (http://www.doughellmann.com/PyMOTW/)') body = str(form) req.add_header('Content-type', form.get_content_type()) req.add_header('Content-length', len(body)) req.add_data(body) response = opener.open(req) response.read() ###################################################################
form.add_field('id', 'interna_broken_link_%s' % timestamp) form.add_field( 'title', 'Internal Broken Link Report %s-%s-%s' % (now.year, now.month, now.day)) else: form.add_field('id', 'total_broken_link_%s' % timestamp) form.add_field( 'title', 'Total Broken Link Report %s-%s-%s' % (now.year, now.month, now.day)) form.add_field('form_submit', 'Save') form.add_field('form.submitted', '1') # Add the file f = open(file_to_publish, 'r') form.add_file('file_file', 'checklink_output.html', fileHandle=f) f.close() # Build the request req = urllib2.Request(url) req.add_header('User-agent', 'PyMOTW (http://www.doughellmann.com/PyMOTW/)') body = str(form) req.add_header('Content-type', form.get_content_type()) req.add_header('Content-length', len(body)) req.add_data(body) response = opener.open(req) response.read() ###################################################################