Пример #1
0
def voice(file_path, apikey):
    time.sleep(SLEEP_TIME)
    register_openers()
    items = [
        MultipartParam('apikey', apikey),
        MultipartParam.from_file('wav', file_path)
    ]
    datagen, headers = multipart_encode(items)
    request = urllib2.Request(EMPATH_URL, datagen, headers)
    readObject = urllib2.urlopen(request)  # type: object

    if readObject.getcode() != 200:
        print("HTTP status %d" % (readObject.getcode()))

    response = json.load(readObject)
    print(response)

    if response['error'] != 0:
        print("analyze error %d" % (response['error']))
        return

    anger_num = response['anger'] * (MAX_RANGE // 50)
    print(anger_num)
    print(format(anger_num, 'x'))
    while True:
        if subprocess.check_call([
                "gatttool", "-b", "24:0A:C4:07:84:3E", "--char-write-req",
                "-a", "0x002a", "-n",
                format(anger_num, 'x')
        ]) == 0:
            break
Пример #2
0
def send_image(recipient_id, board, player):
    logging.info("Sending image to %r", recipient_id)
    headers = {
        "Content-Type": "application/json"
    }
    message = {
        "attachment": {
            "type": "image",
            "payload": {}
        }
    }

    from poster.encode import multipart_encode, MultipartParam

    payload  = []
    payload.append(MultipartParam('recipient', '{"id":"%s"}' % recipient_id))
    payload.append(MultipartParam('message', json.dumps(message)))
    pic = svg.png_board(board, player)
    payload.append(MultipartParam('filedata', filename="test.png", filetype='image/png', fileobj=pic))
    payload.append(MultipartParam('messaging_type', 'MESSAGE_TAG'))
    payload.append(MultipartParam('tag', 'GAME_EVENT'))

    
    data = multipart_encode(payload)

    r = urlfetch.fetch("https://graph.facebook.com/v2.6/me/messages?access_token=%s" % ACCESS_TOKEN,
                       method=urlfetch.POST, headers=data[1], payload="".join(data[0]))
    if r.status_code != 200:
        logging.error("Error sending svg: %r", r.status_code)
        logging.error("%s" % r.__dict__)
Пример #3
0
def poster_multipart(url, additionalParams, filename):
    # Register the streaming http handlers with urllib2
    #register_openers()
    username = ConfigSectionMap("Mascot")['username']
    password = ConfigSectionMap("Mascot")['password']
    login_url = MASCOT_CGI + '/login.pl'  #ConfigSectionMap("Mascot")['login_url']
    #To include cookie handling
    #opener = register_openers()
    #opener.add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))

    cookiejar = cookielib.CookieJar()
    loginOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))

    params = urllib.urlencode({
        'username': username,
        'password': password,
        'action': 'login'
    })

    request = urllib2.Request(login_url, params)
    login = loginOpener.open(request)
    #print login.read()

    # Upload File
    # use the login cookie for file upload
    register_openers(cookiejar=cookiejar)
    # Start the multipart/form-data encoding of the file "DSC0001.jpg"
    # "image1" is the name of the parameter, which is normally set
    # via the "name" parameter of the HTML <input> tag.
    # headers contains the necessary Content-Type and Content-Length
    # datagen is a generator object that yields the encoded parameters
    items = []
    #wrap post parameters
    for name, value in additionalParams.items():
        items.append(MultipartParam(name, value))
    #add file
    #fileobj=open(filename, "rb")
    #MultipartParam(name, value=None, filename=None, filetype=None, filesize=None, fileobj=None, cb=None)
    items.append(MultipartParam.from_file('FILE', filename))
    #items.append(MultipartParam('infile', open(filename, "r"),filetype='text/plain; charset=utf8'))
    # Data for MS/MS ion searches must be supplied as an ASCII file
    #items.append(MultipartParam('FILE',fileobj ,filetype='application/octet-stream'))

    datagen, headers = multipart_encode(items)
    user_agent = 'Mozilla/5.0'
    headers['User-Agent'] = user_agent
    #headers['Content-Transfer-Encoding']= 'base64'

    #s = "".join(datagen)
    # Create the Request object
    #request = urllib2.Request(url, b2a_base64(s), headers)
    request = urllib2.Request(url, datagen, headers)

    #To add basic authentication to the request
    #auth = base64.encodestring('%s:%s' % (username, password))[:-1] # This is just standard un/pw encoding
    #request.add_header('Authorization', 'Basic %s' % auth ) # Add Auth header to request

    # Actually do the request, and get the response

    return urllib2.urlopen(request)
Пример #4
0
def reply_with_selfie_drafts(recipient_id, img):
    badge = Image.open("assets/" + user_db[recipient_id]["template"] + ".png")
    badge.thumbnail(img.size)

    # pasting badge on bottom right edge
    img.paste(badge,
              (img.size[0] - badge.size[0], img.size[1] - badge.size[1]),
              badge)

    temp_filepath = 'assets/selfie-' + str(uuid.uuid4()) + '.png'
    img.save(temp_filepath)

    params = {"access_token": access_token}

    data = {
        "recipient[id]": recipient_id,
        "message[attachment][type]": "image",
        "message[attachment][payload][]": ""
    }
    items = []

    for name, value in data.items():
        items.append(MultipartParam(name, value))
    items.append(MultipartParam.from_file("file", temp_filepath))

    url = "https://graph.facebook.com/v2.6/me/messages?" + urllib.urlencode(
        params)

    datagen, headers = multipart_encode(items)
    request = urllib2.Request(url, datagen, headers)
    print "#### REQUEST"
    response = urllib2.urlopen(request)

    print response.read()
    os.remove(temp_filepath)
Пример #5
0
    def make_request(self, path, data=None, ajax=False, debug=True):
        url = path if path.startswith("http") else self.url + path
        if ajax:
            url += '&ajax=true' if '?' in url else '?ajax=true'
        request = None
        if data:
            items = []
            # wrap post parameters
            for name, value in data.items():
                if isinstance(value, file):
                    # add file
                    items.append(MultipartParam.from_file(name, value.name))
                else:
                    items.append(MultipartParam(name, value))
            datagen, headers = multipart_encode(items)
            request = urllib2.Request(url, datagen, headers)
        else:
            request = urllib2.Request(url=url)

        if ajax:
            request.add_header('X_REQUESTED_WITH', 'XMLHttpRequest')
        try:
            # return urllib2.urlopen(request)
            return self.opener.open(request)
        except urllib2.HTTPError as ex:
            if not debug:
                raise
            logger.info('error in request to %s' % path)
            logger.info(ex.reason)
            logger.info(ex.read())
            raise
Пример #6
0
 def encode_one(k, v):
     if isinstance(v, cls.FileUpload):
         return MultipartParam(k,
                               value=v.data,
                               filename=v.filename,
                               filetype=v.content_type)
     else:
         k = cls._to_data(k, "encode_one k")
         v = cls._to_data(v, "encode_one v")
         return MultipartParam(k, value=v)
Пример #7
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)
Пример #8
0
def send_request(url, params=None, files=None, method=None):
    """
    Send a request to the SARA Life Science Grid Portal, using the provided arguments. Returns text content.
    @param url: url to request / submit to
    @param params: dictionary of parameters that should be POSTed to the url (optional)
    @param files: dictionary of files that should be POSTed to the url (optional)
    @param method: string HTTP method (optional: POST when params of files are provided, GET otherwise)
    """
    #Encode data
    data = None
    headers = {}
    multipart_params = []
    if params:
        for key, value in params.iteritems():
            multipart_params.append(MultipartParam(key, value))
    if files:
        for key, value in files.iteritems():
            multipart_params.append(MultipartParam.from_file(key, value))
    if multipart_params:
        data, headers = multipart_encode(multipart_params)

    #Create request
    headers.update(Accept='text/plain')
    request = urllib2.Request(url=url, headers=headers, data=data)

    # Set method, which could be DELETE
    if method:
        request.get_method = lambda: method

    # Add authentication, explicitly not using the urllib2.HTTPBasicAuthHandler, as it caused frequent failures
    if 'lsg_username' not in os.environ or 'lsg_password' not in os.environ:
        _load_lsg_credentials()
    base64string = base64.encodestring(os.environ['lsg_username'] + ':' +
                                       os.environ['lsg_password']).replace(
                                           '\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)

    #Send request over opener and retrieve response
    try:
        response = URLLIB2_OPENER.open(request, timeout=180)
    except urllib2.HTTPError as err:
        print url
        print err
        for key in sorted(err.hdrs.keys()):
            print key, err.hdrs[key]
        raise err

    #Retrieve
    content = response.read()
    response.close()
    return content
Пример #9
0
def analyse():
    wav = request.files['wav'].stream.read()
    api_key = request.form['apikey']

    register_openers()
    items = []
    items.append(MultipartParam('apikey', api_key))
    items.append(MultipartParam('wav', wav))
    datagen, headers = multipart_encode(items)
    app.url = configenv.API_URL
    callRequest = urllib2.Request(app.url, datagen, headers)
    response = urllib2.urlopen(callRequest)

    return (response.read())
Пример #10
0
def voice_rgb(file_path, apikey):
    time.sleep(SLEEP_TIME)
    register_openers()
    items = [
        MultipartParam('apikey', apikey),
        MultipartParam.from_file('wav', file_path)
    ]
    datagen, headers = multipart_encode(items)
    request = urllib2.Request(EMPATH_URL, datagen, headers)
    readObject = urllib2.urlopen(request)  # type: object

    if readObject.getcode() != 200:
        print("HTTP status %d" % (readObject.getcode()))

    response = json.load(readObject)
    print(response)

    if response['error'] != 0:
        print("analyze error %d" % (response['error']))
        return

    list = [('anger', response['anger']), ('joy', response['joy']),
            ('sorrow', response['sorrow']), ('calm', response['calm'])]
    max_num = max(list, key=max_func)
    if max_num[0] == 'calm':
        rgb = int('ffffff', 16)
    else:
        red = (response['anger'] + response['energy'] / 2) * (MAX_RANGE // 50)
        if red > 255:
            red = 255
        green = (response['joy'] + response['energy'] / 2) * (MAX_RANGE // 50)
        if green > 255:
            green = 255
        blue = (response['sorrow'] + response['energy'] / 2) * (MAX_RANGE //
                                                                50)
        if blue > 255:
            blue = 255
        rgb = (red << 16) + (green << 8) + blue

    print("rgb=", rgb)

    while True:
        if subprocess.check_call([
                "gatttool", "-b", "24:0A:C4:07:84:3E", "--char-write-req",
                "-a", "0x002a", "-n",
                format(rgb, 'x')
        ]) == 0:
            break
Пример #11
0
    def upload_file(self, name, fd):
        assert _FILEUPLOAD, "poster needs to be installed, hg+https://bitbucket.org/chrisatlee/poster"

        if not self._authenticated:
            self._authenticate()

        url = self._url + "/file/upload"

        params = (MultipartParam(name='Filedata',
                                 fileobj=fd,
                                 filename=name,
                                 filetype='application/octet-stream'), )

        datagen, headers = multipart_encode(params)
        request = Request(url, datagen, headers)

        opener = register_openers()
        opener.addheaders.append(('Cookie', 'token=%s' % self._token))

        reply = opener.open(request)

        dom = parse_xml(reply)

        reply_dom = dom.getElementsByTagName('reply')[0]

        status = get_text_by_tag(reply_dom, 'status')
        if status != 'OK':
            raise Exception("Upload Failed")

        return reply
Пример #12
0
    def upload_img(self, logo_url):
        image_url = logo_url
        filetype = 'image/%s' % image_url.split('.')[-1]
        if len(filetype) > 10:
            filetype = 'image/jpg'

        filename = image_url.split('/')[-1]
        raw_img = None

        result = urlfetch.fetch(image_url)
        if result.status_code == 200:
            raw_img = result.content
        else:
            return "error fetching URL"

        multipart_param = MultipartParam('file',
                                         raw_img,
                                         filename=filename,
                                         filetype=filetype)
        datagen, headers = multipart_encode([multipart_param])
        upload_url = blobstore.create_upload_url('/upload_image')
        result = urlfetch.fetch(url=upload_url,
                                payload="".join(datagen),
                                method=urlfetch.POST,
                                headers=headers)

        blob = blobstore.get(json.loads(result.content)["key"])

        self.logo = blob.key()
        self.put()
Пример #13
0
    def test_post_new_attachment(self):
        content_type = 'text/plain'
        filename = 'filename.txt'
        data = 'pretend to be binary attachment data'
        file = MultipartParam('attachment',
                              value=data,
                              filename=filename,
                              filetype=content_type)
        datagen, headers = multipart_encode([file])
        post_data = "".join(datagen)

        _, req = yield self.post_attachment(post_data, headers)

        self.assertEqual(201, req.code)
        self.assertEqual(
            '/attachment/B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A',
            req.headers['Location'])
        response_json = {
            'ident':
            'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A',
            'content-type': content_type,
            'name': filename,
            'size': len(data),
            'encoding': 'base64'
        }
        self.assertEqual(response_json, json.loads(req.written[0]))
Пример #14
0
    def make_request(self, path, data=None,
                     ajax=False, debug=True):
        url = path if path.startswith("http") else self.url + path
        if ajax:
            url += '&ajax=true' if '?' in url else '?ajax=true'
        request = None
        if data:
            items = []
            # wrap post parameters
            for name, value in data.items():
                if isinstance(value, file):
                    # add file
                    items.append(MultipartParam.from_file(name, value.name))
                else:
                    items.append(MultipartParam(name, value))
            datagen, headers = multipart_encode(items)
            request = urllib2.Request(url, datagen, headers)
        else:
            request = urllib2.Request(url=url)

        if ajax:
            request.add_header('X_REQUESTED_WITH', 'XMLHttpRequest')
        try:
            # return urllib2.urlopen(request)
            return self.opener.open(request)
        except urllib2.HTTPError as ex:
            if not debug:
                raise
            logger.error('error in request to %s' % path)
            logger.error(ex.reason)
            logger.error(ex.read())
            raise
Пример #15
0
def postFileRequest(url,
                    paramName,
                    fileObj,
                    additionalHeaders={},
                    additionalParams={}):
    items = []
    # wrap post parameters
    for name, value in additionalParams.items():
        items.append(MultipartParam(name, value))
    # add file
    items.append(MultipartParam.from_file(paramName, fileObj))
    datagen, headers = multipart_encode(items)
    # add headers
    for item, value in additionalHeaders.iteritems():
        headers[item] = value
    return urllib2.Request(url, datagen, headers)
Пример #16
0
    def add_attachment(self,
                       project_id,
                       story_id,
                       filename,
                       file_obj,
                       filetype,
                       filesize=None):
        if isinstance(file_obj, basestring):
            file_obj = open(file_obj, 'rb')
        file_data = MultipartParam(name='Filedata',
                                   filename=filename,
                                   filetype=filetype,
                                   fileobj=file_obj,
                                   filesize=filesize)

        params = {'Filedata': file_data}
        data, mp_headers = multipart_encode(params)

        if 'Content-Length' in mp_headers:
            mp_headers['Content-Length'] = str(mp_headers['Content-Length'])

        return self._post("projects/%s/stories/%s/attachments" %
                          (project_id, story_id),
                          body="".join(list(data)),
                          headers=mp_headers)
Пример #17
0
def wechat_file_upload(access_token,
                       src_file_url,
                       filename="image.jpg",
                       filetype="image/jpg"):
    """
    微信文件上传
    :param access_token: 访问码
    :param src_file_url: 源url
    :param filename:  文件名: image.jpg
    :param filetype:  文件类型: image/jpg
    :return:  {} 上传结果
    """
    # 在 urllib2 上注册 http 流处理句柄
    register_openers()

    logger.debug('wechat_file_upload: src_file_url=%s', src_file_url)

    # 将beiqi文件上传到微信文件服务器,并获取media_id
    wechat_upload_url = WECHAT_UPLOAD_URL % (access_token,
                                             filetype.split("/")[0])
    file_stream = StringIO.StringIO(urllib2.urlopen(src_file_url).read())
    param = MultipartParam(name='media',
                           filename=filename,
                           filetype=filetype,
                           fileobj=file_stream)
    datagen, headers = multipart_encode({"media": param})
    request = urllib2.Request(wechat_upload_url, datagen, headers)
    logger.debug('wechat_file_upload: wechat_upload_url=%s', wechat_upload_url)
    upload_result = json.loads(urllib2.urlopen(request).read())
    logger.debug(
        'wechat_file_upload: wechat_upload_url=%s, upload_result = %r',
        wechat_upload_url, upload_result)
    return upload_result
Пример #18
0
 def post(self, url, params, boundary):
     headers = get_headers(params, boundary)
     headers['Cookie'] = self.hub_cookie
     params = MultipartParam.from_params(params)
     return self.hub_pool.urlopen('POST',
                                  url,
                                  MultipartReader(params, boundary),
                                  headers=headers)
Пример #19
0
 def post(self, url, params, boundary):
     headers = get_headers(params, boundary)
     headers['Cookie'] = self.hub_cookie
     params = MultipartParam.from_params(params)
     return self.hub_pool.urlopen('POST',
                                  url,
                                  MultipartReader(params, boundary),
                                  headers=headers)
Пример #20
0
def voice(file_path, apikey):
    time.sleep(5)
    url = "https://api.webempath.net/v2/analyzeWav"
    register_openers()
    items = [MultipartParam('apikey', apikey),
             MultipartParam.from_file('wav', file_path)]
    datagen, headers = multipart_encode(items)
    request = urllib2.Request(url, datagen, headers)
    readObject = urllib2.urlopen(request)  # type: object

    if readObject.getcode() != 200:
        print("HTTP status %d" % (readObject.getcode()))

    response = json.load(readObject)
    print(response)
    if response["error"] > 0:
        print(response["error"])
Пример #21
0
    def do_url_request(self, api_call, multipart=False, data=None, files=[],
                       **kwargs):
        """
        Issues a url request.
        """
        # Read the credentials from the config file (.transifexrc)
        home = os.getenv('USERPROFILE') or os.getenv('HOME')
        txrc = os.path.join(home, ".transifexrc")
        config = ConfigParser.RawConfigParser()

        if not os.path.exists(txrc):
            print "Cannot find the ~/.transifexrc!"
            raise ProjectNotInit()

        # FIXME do some checks :)
        config.read(txrc)
        username = config.get('API credentials', 'username')
        passwd = config.get('API credentials', 'password')
        token = config.get('API credentials', 'token')
        hostname = config.get('API credentials', 'hostname')

        # Create the Url
        kwargs['hostname'] = hostname
        url = API_URLS[api_call] % kwargs

        password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
        password_manager.add_password(None, hostname, username,passwd)
        auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)

        opener = None
        headers = None
        req = None
        if multipart:
            # Register the streaming http handlers with urllib2
            opener = register_openers()
            opener.add_handler(auth_handler)

            file_params = []
            # iterate through 2-tuples
            for f in files:
                file_params.append(MultipartParam.from_file(f[0], f[1]))
            # headers contains the necessary Content-Type and Content-Length
            # data is a generator object that yields the encoded parameters
            data, headers = multipart_encode(file_params)
            req = urllib2.Request(url=url, data=data, headers=headers)
            # FIXME: This is used till we have a fix from Chris.
            base64string = base64.encodestring('%s:%s' % (username, passwd))[:-1]
            authheader =  "Basic %s" % base64string
            req.add_header("Authorization", authheader)
        else:
            opener = urllib2.build_opener(auth_handler)
            urllib2.install_opener(opener)
            req = urllib2.Request(url=url, data=data)

        fh = urllib2.urlopen(req)
        raw = fh.read()
        fh.close()
        return raw
Пример #22
0
    def test_multipart_encode_list(self):
        with tempfile.NamedTemporaryFile('w+b') as file:
            file.write(b'world')
            file.flush()

            param = MultipartParam.from_file('hello', file)

            self.assertIsNotNone(param)
            print(param)
Пример #23
0
def generate_multipart_photo(qqid, skey, full_filename, boundary):
    filename =  os.path.split(full_filename)[-1].split(".")[0]
    image_param = None
    try:
        image_param = MultipartParam.from_file("filename", full_filename)
    except OSError, e:
        if e.errno == 2:
            log_paperboy("File not found %s" % full_filename)
        raise e
Пример #24
0
def post_article(params):
    #绑定cookie,自动处理cookie
    cj = cookielib.LWPCookieJar()
    #生成一个带cookie的opener,可以使用opener.open()打开URL
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    #安装opener,使得可以直接用urllib2.urlopen()带上cookie
    urllib2.install_opener(opener)
    #获取csrfmiddlewaretoken
    csrfmiddlewaretoken = get_csrf(
        'http://121.42.175.88/admin/login/?next=/admin/')
    username = {{yours}}
    pwd = {{yours}}
    log_in(username, pwd, csrfmiddlewaretoken)

    #获取add页面的csrf
    csrfmiddlewaretoken = get_csrf(
        'http://121.42.175.88/admin/blog/article/add/')

    params['csrfmiddlewaretoken'] = csrfmiddlewaretoken

    #print params

    items = []
    for key, value in params.items():
        items.append(MultipartParam(key, value))

    opener = poster.streaminghttp.register_openers()
    opener.add_handler(urllib2.HTTPCookieProcessor(cj))

    #这里只有name-value对,直接用dict就可以成功
    #stackflow上提到如果上传键值对和文件,键值对先要封装成MultipartParam类
    #这里我持怀疑态度,官方说MultipartParam类处理独立的一对键值对
    #这里使用封装后的items也可以的
    datagen, headers = multipart_encode(params)

    additionalHeaders = {
        'Accept-Language':
        'zh-CN,zh;q=0.8',
        'Connection':
        'keep-alive',
        'Host':
        '121.42.175.88',
        'Origin':
        'http://121.42.175.88',
        'Referer':
        'http://121.42.175.88/admin/blog/article/add/',
        'User-Agent':
        'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'
    }

    for key, value in additionalHeaders.iteritems():
        headers[key] = value

    req = urllib2.Request('http://121.42.175.88/admin/blog/article/add/',
                          datagen, headers)

    ret = urllib2.urlopen(req)
Пример #25
0
def generate_multipart_photo(qqid, skey, full_filename, boundary):
    filename = os.path.split(full_filename)[-1].split(".")[0]
    image_param = None
    try:
        image_param = MultipartParam.from_file("filename", full_filename)
    except OSError, e:
        if e.errno == 2:
            log_paperboy("File not found %s" % full_filename)
        raise e
Пример #26
0
def post_file(url,
              file_name,
              file_type,
              file_size,
              file_obj,
              options=dict(),
              username=None,
              password=None):

    # Input checks
    if url is None:
        raise ValueError('url')

    if file_name is None:
        raise ValueError('file_name')

    if file_type is None:
        raise ValueError('file_type')

    if file_size is None:
        raise ValueError('file_size')

    if file_obj is None:
        raise ValueError('file_obj')

    if options is None:
        raise ValueError('options')

    logging.debug('Preparing file {0}'.format(file_name))

    # This is the post arguments section
    options['file'] = MultipartParam('file',
                                     filename=file_name,
                                     filetype=file_type,
                                     filesize=file_size,
                                     fileobj=file_obj)

    data, headers = multipart_encode(options)

    logging.debug('Submitting the file to {0}'.format(url))

    # For authorization (optional)
    if username is not None and password is not None:
        headers['Authorization'] = generate_authorization_header(
            username, password)

    fetch = urlfetch.fetch(url=url,
                           payload="".join(data),
                           method=urlfetch.POST,
                           headers=headers)
    response = fetch.content

    return response
Пример #27
0
    def submit_old(self,
                   text="TEXT TO DELETE",
                   tag=blinepython,
                   file=None,
                   file_descr="description not known",
                   file2=None,
                   file_descr2="description not known",
                   file3=None,
                   file_descr3="description not known",
                   runnum=None):
        #def submit(self,text="TEXT TO DELETE",tag="sxrpython",file=None,file_descr="description not known",file2=None,file_descr2="description not known",file3=None,file_descr3="description not known"):
        exper_id = self.logbook_experiments[self.experiment]['id']
        url = ws_url + '/LogBook/iNewFFEntry4grabberJSON.php'
        if (file is None):
            data = (('author_account', logbook_author), ('id', exper_id),
                    ('message_text', text), ('scope', 'experiment'),
                    ('num_tags', '1'), ('tag_name_0',
                                        blinepython), ('tag_value_0', tag))
        elif (file2 is None):
            data = (('author_account', logbook_author), ('id', exper_id),
                    ('message_text', text), ('scope', 'experiment'),
                    ('num_tags', '1'), ('tag_name_0',
                                        blinepython), ('tag_value_0', tag),
                    MultipartParam.from_file("file1",
                                             file), ("file1", file_descr))
        elif (file3 is None):
            data = (('author_account', logbook_author), ('id', exper_id),
                    ('message_text', text), ('scope', 'experiment'),
                    ('num_tags', '1'), ('tag_name_0',
                                        blinepython), ('tag_value_0', tag),
                    MultipartParam.from_file("file1",
                                             file), ("file1", file_descr),
                    MultipartParam.from_file("file2",
                                             file2), ("file2", file_descr2))
        else:
            data = (('author_account', logbook_author), ('id', exper_id),
                    ('message_text', text), ('scope', 'experiment'),
                    ('num_tags', '1'), ('tag_name_0',
                                        blinepython), ('tag_value_0', tag),
                    MultipartParam.from_file("file1",
                                             file), ("file1", file_descr),
                    MultipartParam.from_file("file2",
                                             file2), ("file2", file_descr2),
                    MultipartParam.from_file("file3",
                                             file3), ("file3", file_descr3))

        datagen, headers = multipart_encode(data)

        try:
            req = urllib2.Request(url, datagen, headers)
            response = urllib2.urlopen(req)
            the_page = response.read()
            result = simplejson.loads(the_page)
            if result['status'] != 'success':
                tkMessageBox.showerror("Error", result['message'])
            new_message_id = int(result['message_id'])

        except urllib2.URLError, reason:
            tkMessageBox.showerror("Submit New Message Error", reason)
Пример #28
0
def submit(outputfile):
    items = []
    items.append(MultipartParam('logtype', 'Detailed'))
    items.append(MultipartParam('hwid', 'HW4'))
    items.append(MultipartParam.from_file('infile', trecEvalOutputPathFolder + outputfile))
    datagen, headers = multipart_encode(items)
    base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
    headers['Authorization'] = "Basic %s" % base64string
    request = urllib2.Request(url, datagen, headers)
    result = urllib2.urlopen(request)
    t = result.read()
    map_all, P10, P20, P30, win, loss, mapdict = findMAP(t)
    return map_all, P10, P20, P30, win, loss
Пример #29
0
 def upload(self, url, path):
     if isinstance(path, unicode):
         name = path
         path = path.encode("utf-8")
     else:
         name = path.decode("utf-8")
     with open(path, "rb") as f:
         mp = MultipartParam("filename", fileobj=f, filename=name)
         datagen, headers = multipart_encode({"filename": mp})
         # Create the Request object
         request = urllib2.Request(url, datagen, headers)
         res = json.load(self.opener.open(request))
     return res
Пример #30
0
 def testRun():
     response = ''
     try:
         register_openers()
         p = MultipartParam("file", "0123456789abcdef", "test.txt", "text/plain; charset=utf8")
         datagen, headers = multipart_encode( [("var4", "val with spaces"), p] )
         request = urllib2.Request('http://etd.local/upload', datagen, headers)
         response = urllib2.urlopen(request, None, 2).read()
     except:
         return 1
     if response != 'test.txt:16&var4=val with spaces':
         return 1
     return 0
Пример #31
0
    def post(self, post_id):
        if post_id == '1':
            blog = models.BlogPost()
            blog.title = self.request.get('title')
            blog.subtitle = self.request.get('subtitle')
            blog.author = self.request.get('author')
            blog.brief = self.request.get('brief')
            blog.content = self.request.get('content')
            blog.category = self.request.get('category').split(',')
            blog.put()
        else:
            blog = models.BlogPost.get_by_id(long(post_id))
            if blog is not None:
                blog.title = self.request.get('title')
                blog.subtitle = self.request.get('subtitle')
                blog.author = self.request.get('author')
                blog.brief = self.request.get('brief')
                blog.content = self.request.get('content')
                blog.category = self.request.get('category').split(',')
                blog.put()

        if hasattr(self.request.POST['file'], 'filename'):
            #re-post to blobstore, documented at: https://code.google.com/p/googleappengine/issues/detail?id=2749#makechanges
            from google.appengine.api import urlfetch
            from poster.encode import multipart_encode, MultipartParam
            payload = {}
            file_data = self.request.POST['file']
            payload['file'] = MultipartParam('file',
                                             filename=file_data.filename,
                                             filetype=file_data.type,
                                             fileobj=file_data.file)
            data, headers = multipart_encode(payload)
            upload_url = blobstore.create_upload_url('/admin/blog/upload/%s/' %
                                                     blog.key.id())
            t = urlfetch.fetch(url=upload_url,
                               payload="".join(data),
                               method=urlfetch.POST,
                               headers=headers)

            #output toast message
            if t.content == 'success':
                self.add_message(messages.saving_success, 'success')
                return self.redirect_to('admin-blog')
            else:
                self.add_message(messages.saving_error, 'danger')
                return self.get(post_id=blog.key.id())
        else:
            self.add_message(messages.saving_success, 'success')
            return self.redirect_to('admin-blog')
Пример #32
0
def send_request(url, params=None, files=None, method=None):
    """
    Send a request to the SARA Life Science Grid Portal, using the provided arguments. Returns text content.
    @param url: url to request / submit to
    @param params: dictionary of parameters that should be POSTed to the url (optional)
    @param files: dictionary of files that should be POSTed to the url (optional)
    @param method: string HTTP method (optional: POST when params of files are provided, GET otherwise)
    """
    #Encode data
    data = None
    headers = {}
    multipart_params = []
    if params:
        for key, value in params.iteritems():
            multipart_params.append(MultipartParam(key, value))
    if files:
        for key, value in files.iteritems():
            multipart_params.append(MultipartParam.from_file(key, value))
    if multipart_params:
        data, headers = multipart_encode(multipart_params)

    #Create request
    headers.update(Accept='text/plain')
    request = urllib2.Request(url=url, headers=headers, data=data)

    # Set method, which could be DELETE
    if method:
        request.get_method = lambda: method

    # Add authentication, explicitly not using the urllib2.HTTPBasicAuthHandler, as it caused frequent failures
    if 'lsg_username' not in os.environ or 'lsg_password' not in os.environ:
        _load_lsg_credentials()
    base64string = base64.encodestring(os.environ['lsg_username'] + ':' + os.environ['lsg_password']).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)

    #Send request over opener and retrieve response
    try:
        response = URLLIB2_OPENER.open(request, timeout=180)
    except urllib2.HTTPError as err:
        print url
        print err
        for key in sorted(err.hdrs.keys()):
            print key, err.hdrs[key]
        raise err

    #Retrieve
    content = response.read()
    response.close()
    return content
Пример #33
0
def send_photo(data, filename, caption):
    logging.info("Sending photo: %s", filename)
    payload = {}
    payload['userId'] = 'test'
    payload['caption'] = caption
    payload['photoFile'] = MultipartParam('photoFile',
                                          filename=filename,
                                          filetype='image/jpeg',
                                          fileobj=cStringIO.StringIO(data))

    data, headers = multipart_encode(payload)
    urlfetch.fetch(url='https://friggr.unlessquit.com/inbox',
                   method=urlfetch.POST,
                   payload="".join(data),
                   headers=headers)
def sendPhoto( chatid, photofilename ):
	try:

		items = []
		items.append(MultipartParam('chat_id', chatid))
		items.append(MultipartParam.from_file('photo', photofilename))
		datagen, headers = multipart_encode(items)
		req = Request('https://api.telegram.org/{bot}:{api key}/sendPhoto', datagen, headers)
		print req
		rsp = urlopen(req)
		print rsp
        except Exception, e:
                print 'send photo error'
                print e
		print URLError
Пример #35
0
    def process(self, inbox):
        """
        Inbox is expected to be a list of imap.Message objects.
        Although any list of mapping objects is accepted, provided
        that objects support methods enlisted in 'actions' option
        """

        # Poster: Register the streaming http handlers with urllib2
        register_openers()

        for message in inbox:
            res = self.map(message)
            if not res:
                continue
            url, options = res
            for action in options['actions']:
                getattr(message, action)()
            files = []
            if options['send_files']:
                for num, attachment in enumerate(message.attachments):
                    filename, ctype, fileobj = attachment
                    file_param = MultipartParam('attachment[%d]' % num,
                                                filename=filename,
                                                filetype=ctype,
                                                fileobj=fileobj)
                    files.append(file_param)
            data = {}
            for name in options['msg_params']:
                part = message.get(name, None)
                if not part:
                    part = getattr(message, name, None)
                if part: #TODO: maybe we should raise an exception
                         #if there's no part
                    data[name] = part
            data.update(options['add_params'])
            data = MultipartParam.from_params(data)
            data += files
            datagen, headers = multipart_encode(data)
            request = urllib2.Request(url, datagen, headers)
            if options.get('auth', None):
                cj, urlopener = auth.authenticate(options['auth'], request, 
                                                  self.base_url)
            try:
                result = urllib2.urlopen(request).read()
            except urllib2.URLError, e:
                result = e
                #continue    # TODO Log error and proceed.
            yield url, result
Пример #36
0
    def upload_qr_code(deployment, qrcodeimg, image_type):
        multipart_param = MultipartParam('file',
                                         qrcodeimg,
                                         filename='test-qr-code.png',
                                         filetype=image_type)
        datagen, headers = multipart_encode([multipart_param])
        upload_url = blobstore.create_upload_url('/upload_image')

        result = urlfetch.fetch(url=upload_url,
                                payload="".join(datagen),
                                method=urlfetch.POST,
                                headers=headers)

        blob = blobstore.get(json.loads(result.content)["key"])
        deployment.sample_qr_code = blob.key()
        deployment.put()
Пример #37
0
def wechat_file_up(beiqi_down_ful, fn):
    file_type_flag = fn.split('.')[-1]
    filetype='voice/%s' % (file_type_flag)
    print "filetype,",filetype
    param = MultipartParam(name='media', filename=fn, filetype=filetype, fileobj=StringIO.StringIO(urllib2.urlopen(beiqi_down_ful).read()))
    datagen, headers = multipart_encode({"media": param})
    print "datagen,",datagen
    print "headers,",headers

    # 创建请求对象
    type='voice'
    request = urllib2.Request("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s"%(access_token, type), datagen, headers)

    # 实际执行请求并取得返回
    upload_result = urllib2.urlopen(request).read()
    return json.loads(upload_result)
Пример #38
0
    def upload_qr_code_zip(self, qr_code_zip, file_type):
        multipart_param = MultipartParam('file',
                                         qr_code_zip,
                                         filename='qr_codes.zip',
                                         filetype=file_type)
        datagen, headers = multipart_encode([multipart_param])
        upload_url = blobstore.create_upload_url('/upload_image')

        result = urlfetch.fetch(url=upload_url,
                                payload="".join(datagen),
                                method=urlfetch.POST,
                                headers=headers)

        blob = blobstore.get(json.loads(result.content)["key"])
        self.qr_codes_zip = blob.key()
        self.put()
Пример #39
0
def VKSendMessage(userId, data, dataType):
    resp = 0
    if dataType == "text":
        resp = VkRequest("messages.send", {"user_id": userId, "message": data})
    elif dataType == "image":
        # Get url for uploading
        resp = json.loads(
            VkRequest("photos.getMessagesUploadServer", {"peer_id": userId}))
        url = str(resp["response"]["upload_url"])

        # Upload image to VK server
        register_openers()
        data, headers = multipart_encode(
            [MultipartParam(name="photo", value=data, filename="photo.png")])
        #datagen, headers = multipart_encode({"photo": open('512.png')}) # from file
        request = urllib2.Request(url, "".join(data), headers)
        resp = urllib2.urlopen(request).read()

        # Parse json response
        data = json.loads(resp)

        # Save image on server
        resp = VkRequest("photos.saveMessagesPhoto", {
            "photo": data["photo"],
            "server": data["server"],
            "hash": data["hash"]
        })

        # Parse json response
        data = json.loads(resp)

        # Create attachment data
        owner_id = str(data["response"][0]["owner_id"])
        user_id = str(data["response"][0]["id"])
        attachment = "photo" + owner_id + "_" + user_id

        # Send photo to user
        resp = VkRequest("messages.send", {
            "user_id": userId,
            "message": "",
            "attachment": attachment
        })

    # If message was not sent
    if not resp.isdigit():
        # Error
        pass
Пример #40
0
def submit_msg_to_elog(ws_url, usr, ins, sta, exp, cmd, logbook_experiments, lst_tag=[''], run='', msg_id='', msg='', lst_descr=[''], lst_fname=['']):

    exper_id = logbook_experiments[exp]['id']

    url = ws_url+'/LogBook/NewFFEntry4grabberJSON.php'

    child_output = ''
    if cmd is not None: child_output = os.popen(cmd).read()

    if (run != '') and (msg_id != '') :
        print 'run', run
        print 'message_id', msg_id

        msg = "\nInconsistent input:" \
            + "\nRun number can't be used togher with the parent message ID." \
            + "\nChoose the right context to post the screenshot and try again." 
        print msg
        return

    params = [
        ('author_account', usr),
        ('id'            , exper_id),
        ('message_text'  , msg),
        ('text4child'    , child_output) ]
        #('instrument'    , ins),
        #('experiment'    , exp),

    if run != '' :
        params.append(('scope', 'run'))
        params.append(('run_num', run))

    elif msg_id != '' : 
        params.append(('scope', 'message'))
        params.append(('message_id', msg_id))

    else:
        params.append(('scope', 'experiment'))

    if lst_tag !=[''] :
        params.append(('num_tags'      , str(len(lst_tag))))
        for i,tag in enumerate(lst_tag) :
            s = '%d' % (i)
            params.append(('tag_name_' + s    , tag))
            params.append(('tag_value_' + s   , ''))

    if lst_fname != [''] :
        for i,(fname, descr) in enumerate( zip(lst_fname, lst_descr) ) :
            s = '%d' % (i+1)
            params.append(MultipartParam.from_file('file' + s, fname))
            params.append(('file' + s, descr))

#!!!!!!!!!!!!!!!!!!!!!!!
#    print 'params:', params
#    return {'status': 'error', 'message': 'Bad Error message'}
#    return {'status': 'success', 'message_id': '123456'}
#!!!!!!!!!!!!!!!!!!!!!!!
    

    datagen,headers = multipart_encode(params)

    try:

        #print 'Try to submit message: \nurl: ', url, '\ndatagen:', datagen, '\nheaders:' , headers
        req = urllib2.Request(url, datagen, headers)
        response = urllib2.urlopen(req)
        the_page = response.read()
        result = simplejson.loads(the_page)

        #print 'result:',    result
        #NORMAL: result: {'status': 'success', 'message_id': '125263'}
        #ERROR:  result: {'status': 'error', 'message': 'Run number 285 has not been found. Allowed range of runs is: 2..826.'}

        if result['status'] == 'success':
            print 'New message ID:', result['message_id']
        #else :
        #    print 'Error:', result['message']

        return result

    except urllib2.URLError, reason:
        msg = 'Submit New Message Error ' + str(reason)
        #print msg
        return {'status': 'error', 'message': msg}
Пример #41
0
import poster
from poster.encode import multipart_encode, MultipartParam
from poster.streaminghttp import register_openers
import urllib2
import uuid

# Register the streaming http handlers with urllib2
register_openers()

# Start the multipart/form-data encoding of the file "DSC0001.jpg"
# "image1" is the name of the parameter, which is normally set
# via the "name" parameter of the HTML <input> tag.

# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
file = MultipartParam.from_file("file1", "test.tlog")
file.filetype = 'application/vnd.mavlink.tlog'
params = {
    #'tlog1.tlog': file, # the name doesn't matter if the content type is correct
    #'tlog1.tlog': open("mav.tlog", "rb"),
    'autoCreate': 'true',
    'login': '******',
    'password': '******',
    'email': '*****@*****.**',
    'fullName': 'Bob Bumblesticks',
    'api_key': 'APPID.DEVKEY' # DONT FORGET YOUR KEY
    }
datagen, headers = poster.encode.multipart_encode(MultipartParam.from_params(params) + [file])

# Create the Request object
Пример #42
0
    def post_files(self, files, start, end, uploaded_queue_put, boundary, local_deploy):

        hub_session = self.hub_session
        hub_cookie = self.hub_cookie
        hub_pool = self.hub_pool

        while start < end:
            if self.stopped:
                uploaded_queue_put(None) # Make sure the waiting thread wakes up
                break

            item = files[start]
            start += 1

            deploy_path, relative_path, file_size, file_hash, file_md5 = item

            try:
                if local_deploy:
                    guessed_type = guess_type(relative_path)[0]
                    if guessed_type is None:
                        guessed_type = ""
                    params = {'file.content_type': guessed_type,
                              'file.name': relative_path,
                              'file.path': deploy_path,
                              'session': hub_session,
                              'hash': file_hash,
                              'length': str(file_size),
                              'md5': file_md5}
                    if deploy_path.endswith('.gz'):
                        params['encoding'] = 'gzip'
                    r = hub_pool.request('POST',
                                         '/dynamic/upload/file',
                                          fields=params,
                                          headers={'Cookie': hub_cookie},
                                          timeout=self.hub_timeout)
                else:
                    params = [MultipartParam('file',
                                             filename=relative_path,
                                             filetype=guess_type(relative_path)[0],
                                             fileobj=open(deploy_path, 'rb')),
                              ('session', hub_session),
                              ('hash', file_hash),
                              ('length', file_size),
                              ('md5', file_md5)]
                    if deploy_path.endswith('.gz'):
                        params.append(('encoding', 'gzip'))

                    headers = get_headers(params, boundary)
                    headers['Cookie'] = hub_cookie
                    params = MultipartParam.from_params(params)
                    params = MultipartReader(params, boundary)

                    r = hub_pool.urlopen('POST',
                                         '/dynamic/upload/file',
                                         params,
                                         headers=headers,
                                         timeout=self.hub_timeout)
            except IOError:
                self.stop('Error opening file "%s".' % deploy_path)
                continue
            except (HTTPError, SSLError, ValueError) as e:
                self.stop('Error uploading file "%s": "%s".' % (relative_path, e))
                continue

            if r.headers.get('content-type', '') != 'application/json; charset=utf-8':
                self.stop('Hub error uploading file "%s".' % relative_path)
                continue

            answer = json_loads(r.data)

            # pylint: disable=E1103
            if r.status != 200:
                if answer.get('corrupt', False):
                    self.stop('File "%s" corrupted on transit.' % relative_path)
                else:
                    msg = answer.get('msg', None)
                    if msg:
                        self.stop('Error when uploading file "%s".\n%s' % (relative_path, msg))
                    else:
                        self.stop('Error when uploading file "%s": "%s"' % (relative_path, r.reason))
                continue

            if not answer.get('ok', False):
                self.stop('Error uploading file "%s".' % relative_path)
                continue
            # pylint: enable=E1103

            uploaded_queue_put((relative_path, file_size, file_hash))

            answer = None
            r = None
            params = None
            relative_path = None
            deploy_path = None
            item = None
Пример #43
0
try:
    localFile = open(package_filename, "wb")
    localFile.write(data)
    localFile.close()
except Exception, e:
    print ("failed to create local file, " + str(e))
    sys.exit(1)

#
# upload package file onto TOT
#
register_openers()

# datagen, headers = multipart_encode( { 'file' : open( ipa_filename, 'rb' ), 'changelog' : build_name + '.' + str( build_number ), 'submit' : 'Submit' } )
ipa = MultipartParam.from_file("file", package_filename)
ipa.filetype = "application/octet-stream"
changelog = MultipartParam("changelog", str(build_number))
submit = MultipartParam("submit", "Submit")
datagen, headers = multipart_encode([ipa, changelog, submit])

request = urllib2.Request(config["tot_url"] + "/upload.php", datagen, headers)

print urllib2.urlopen(request).read()

# delete the package
os.remove(package_filename)

#
# save the current build number
#
def get_batch_request():
    batch_request_file = open("batch_request/batch_request.xml")
    batch_request = batch_request_file.read()

    batch_request = batch_request.replace("{{filename}}", "Request.csv")
    batch_request = batch_request.replace("{{application_batch_id}}", get_next_batch_id())
    batch_request = batch_request.replace("{{message_timestamp}}", datetime.datetime.now().isoformat() + "Z")

username = '******'
password = '******'
url = "https://maxcvservices.dnb.com:8443/V2.0/Batches"
token = get_auth_token(username, password)

# Register the streaming http handlers with urllib2
register_openers()

# Start the multipart/form-data encoding of the file "DSC0001.jpg"
# "image1" is the name of the parameter, which is normally set
# via the "name" parameter of the HTML <input> tag.

request_param = MultipartParam.from_params({'request' : })

# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
datagen, headers = MultipartParam.from_file({"image1": open("DSC0001.jpg", "rb")})

# Create the Request object
request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers)
# Actually do the request, and get the response
print urllib2.urlopen(request).read()