def image_to_base64(image): """ Converts PIL.Image onto Base64-encoded string :param PIL.Image image: image to encode :return: Base64-encoded string with image """ buffer = io.BytesIO() image.save(buffer, 'PNG') byte_content = buffer.getvalue() base64_content = util.base64_encode(byte_content) return base64_content
def test_case01(self): url='http://139.199.132.220:9000/event/api/register/' headers = {'Content-Type': 'application/x-www-form-urlencoded'} data = { 'username':'******', 'password': util.base64_encode('huicehuice!@#') } res = requests.post(url=url,headers=headers,data=data) print res.status_code print res.text
def _upload_file(self, infile): """ Uploads the file with the given name. :param string infile: path to file :return: ID of the created Takeoff """ with open(infile, 'rb') as f: byte_content = f.read() base64_content = util.base64_encode(byte_content) payload = dict(filename=infile, content=base64_content) response = self.app.post('/upload_file', data=json.dumps(payload)) result = self._toJson(response) takeoff_id = util.dict_get(result, 'takeoff_id', None) return takeoff_id
def create_connection(self): if self.proxy_host and self.proxy_port: _header = {} if self.proxy_username and self.proxy_password: _header['Proxy-Authorization'] = 'Basic %s' % ( util.base64_encode(str(self.proxy_username) + ':' + str(self.proxy_password))) if self.is_secure: if util.compare_version(sys.version.split()[0], '2.7.9') >= 0: self.connection = httplib.HTTPSConnection(self.proxy_host, port=self.proxy_port, timeout=self.timeout, context=self.myssl.SSLContext( self.sslVersion)) else: self.connection = httplib.HTTPSConnection(self.proxy_host, port=self.proxy_port, timeout=self.timeout) self.connection.set_tunnel(self.host, port=443, headers=_header) else: self.connection = httplib.HTTPConnection(self.proxy_host, port=self.proxy_port, timeout=self.timeout) self.connection.set_tunnel(self.host, port=80, headers=_header) logging.debug('create connection to host: %s by proxy host: %s' % ( self.host, self.proxy_host)) else: if self.is_secure: self.connection = httplib.HTTPSConnection(self.host, port=443, timeout=self.timeout, context=self.myssl.SSLContext( self.sslVersion)) if util.compare_version( sys.version.split()[0], '2.7.9') >= 0 else httplib.HTTPSConnection(self.host, port=443, timeout=self.timeout) else: self.connection = httplib.HTTPConnection(self.host, port=80, timeout=self.timeout) logging.debug('create connection to host: ' + self.host)
def send_message(self, message, prev_return=None): """发送消息""" message_type = "" data = {} err = "" try: send_message_start = time.time() message_type = message.get("type", "") data = message.get("data", {}) err = message.get("err", "") # 如果有上一次(广播相同数据)的返回 # 相同的包名和连接标志 # 而且是无加密或简单加密 # 可以跳过组包, 直接使用上一次的发送数据 if prev_return \ and self.pg_tag == prev_return["tag"] \ and (not self._crypt or self._simple): message_str = prev_return["message_str"] packed = 0 else: packed = 1 self.msg_log(message_type, data=data, err=err) # 序列化 + 加密 if self._crypt: assert self._key, "no aes key! ignore sending message..." random_bytes_length = \ util.crc32(self._package_name + message_type) % 31 + 1 message_str = \ chr(random_bytes_length) + \ util.random_bytes(random_bytes_length) + \ util.aes_encrypt_smart(self._key, self._dumps(message), self._iv) # 序列化 else: message_str = self._dumps(message) # 压缩 if self._compress: # 压缩跳过(长度): 首字节表示是否压缩: \0x00 未压缩 \x01 压缩了 if self._compress_skip_size > 0: if len(message_str) > self._compress_skip_size: message_str = "\x01" + self._compress(message_str) else: message_str = "\x00" + message_str # 正常压缩 else: message_str = self._compress(message_str) # 编码 if self._base64: message_str = util.base64_encode(message_str) try: self.write_message(message_str, binary=self._binary) self.send += 1 self.send_bytes += len(message_str) # 平均发送延迟 # Conn.total_send += 1 Conn.total_send_time += time.time() - send_message_start if Conn.total_send % 10000 == 0: logging.debug( "Conn: total-sent %d messages, avg:%.3fus", Conn.total_send, Conn.total_send_time * 1000000.0 / Conn.total_send ) except: logging.error("send to client error:\n%s", format_exc()) return {"message_str": message_str, "tag": self.pg_tag, "packed": packed} except: self.error( "Exception:\n%son sending message error: type:%r %s %s\n", format_exc(), message_type, "data:%r" % data if data else "", "err:%r" % err if err else "", )