Exemple #1
0
 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 __init__(self,repo, command, callback=None):
     KillableThread.__init__(self)
     self.repo = repo
     self.callback=callback
     global cookiejar
     self.cookiejar = cookiejar
     
     assert SkarphedRepository.COMMANDS.has_key(command['c'])
     
     url = str(repo.getUrl())
     
     json_enc = json.JSONEncoder()
     
     form = MultiPartForm()
     form.add_field('j',json_enc.encode(command))
     
     post = str(form)
     
     self.request = urllib2.Request(url)
     self.request.add_header('User-agent','SkarphedAdmin')
     self.request.add_header('Content-type',form.get_content_type())
     self.request.add_header('Body-length',len(post))
     self.request.add_data(post)
     
     Tracker().addThread(self)
Exemple #3
0
 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()
Exemple #4
0
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()
Exemple #5
0
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()
    urlopen = urllib2.urlopen
    Request = urllib2.Request
    # Set timeout for requests
    socket.setdefaulttimeout(10)

    # Must get a session cookie first
    req = Request(plugin.options.httphost + '/login.cgi')
    if (verbose >= 2):
        print "Opening session"
    resp = urlopen(req)

    # Post the form to login
    form.add_field('uri', '/status.cgi')
    body = str(form)
    req = Request(plugin.options.httphost + '/login.cgi')
    req.add_header('Content-type', form.get_content_type())
    req.add_header('Content-length', len(body))
    req.add_data(body)
    if (verbose >= 2):
        print "Logging in and collecting data"
    resp = urlopen(req)

    # Check we reached the right page after redirection post login
    if (resp.geturl() != plugin.options.httphost + '/status.cgi'):
        if (verbose >= 2):
            print "Login may have failed"
        raise Exception("Reached a wrong page: " + resp.geturl())

    # Check content-type (last resort) before passing to JSON parser
    contype = resp.info()['Content-type']
    if (contype != "application/json"):
    urlopen = urllib2.urlopen
    Request = urllib2.Request
    # Set timeout for requests
    socket.setdefaulttimeout(10)

    # Must get a session cookie first
    req = Request(plugin.options.httphost + "/login.cgi")
    if verbose >= 2:
        print "Opening session"
    resp = urlopen(req)

    # Post the form to login
    form.add_field("uri", "/status.cgi")
    body = str(form)
    req = Request(plugin.options.httphost + "/login.cgi")
    req.add_header("Content-type", form.get_content_type())
    req.add_header("Content-length", len(body))
    req.add_data(body)
    if verbose >= 2:
        print "Logging in and collecting data"
    resp = urlopen(req)

    # Check we reached the right page after redirection post login
    if resp.geturl() != plugin.options.httphost + "/status.cgi":
        if verbose >= 2:
            print "Login may have failed"
        raise Exception("Reached a wrong page: " + resp.geturl())

        # Check content-type (last resort) before passing to JSON parser
    contype = resp.info()["Content-type"]
    if contype != "application/json":
	urlopen = urllib2.urlopen
	Request = urllib2.Request
	# Set timeout for requests
	socket.setdefaulttimeout(10)

	# Must get a session cookie first
	req = Request(plugin.options.httphost + '/login.cgi')
	if (verbose >= 2):
		print "Opening session"
	resp = urlopen(req)

	# Post the form to login
	form.add_field('uri', '/status.cgi')
	body = str(form)
	req = Request(plugin.options.httphost + '/login.cgi')
	req.add_header('Content-type', form.get_content_type())
	req.add_header('Content-length', len(body))
	req.add_data(body)
	if (verbose >= 2):
		print "Logging in and collecting data"
	resp = urlopen(req)

	# Check we reached the right page after redirection post login
 	if (resp.geturl() != plugin.options.httphost + '/status.cgi'):
		if (verbose >= 2):
			print "Login may have failed"
 		raise Exception("Reached a wrong page: " + resp.geturl())

	# Check content-type (last resort) before passing to JSON parser
	contype = resp.info()['Content-type']
	if (contype != "application/json"):