pythonbase模塊
『壹』 python的BaseHTTPServer模塊怎樣接收post請求能給出,把接收到的POST數據輸...
#!/usr/bin/python
#encoding=utf-8
'''
基於BaseHTTPServer的http server實現,包括get,post方法,get參數接收,post參數接收。
'''
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import io,shutil
import urllib
import os, sys
class MyRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
mpath,margs=urllib.splitquery(self.path) # ?分割
self.do_action(mpath, margs)
def do_POST(self):
mpath,margs=urllib.splitquery(self.path)
datas = self.rfile.read(int(self.headers['content-length']))
self.do_action(mpath, datas)
def do_action(self, path, args):
self.outputtxt(path + args )
def outputtxt(self, content):
#指定返回編碼
enc = "UTF-8"
content = content.encode(enc)
f = io.BytesIO()
f.write(content)
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-Length", str(len(content)))
self.end_headers()
shutil.fileobj(f,self.wfile)
『貳』 如何在Python中訪問HBase的數據
Python連接HBase時需要先載入Thrift和HBase的相關包,之後創建與HBase的連接並進行後續操作,具體代碼如下:
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
import pymongo
import hashlib
import time
from datetime import datetime
class HBaseOperator():
def __init__(self):
self.host = "ip_address"
self.port = 9090
self.transport = TBufferedTransport(TSocket(self.host, self.port))
self.transport.open()
self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
self.client = Hbase.Client(self.protocol)
def __del__(self):
self.transport.close()
def getAllTablesInfo(self):
#get table info
listTables = self.client.getTableNames()
print "="*40
print "Show all tables information...."
for tableName in listTables:
print "TableName:" + tableName
print " "
listColumns = self.client.getColumnDescriptors(tableName)
print listColumns
print " "
listTableRegions = self.client.getTableRegions(tableName)
print listTableRegions
print "+"*40
『叄』 Python怎麼能簡單實現Base64編碼和解碼
Base64編碼是一種「防君子不防小人」的編碼方式。廣泛應用於MIME協議,作為電子郵件的傳輸編碼,生成的編碼可逆,後一兩位可能有「=」,生成的編碼都是ascii字元。
優點:速度快,ascii字元,肉眼不可理解
缺點:編碼比較長,非常容易被破解,僅適用於加密非關鍵信息的場合
Python Base64編碼和解碼示例:
>>> import base64
>>> s = '我是字元串'
>>> a = base64.b64encode(s)
>>> print a
ztLKx9fWt/u0rg==
>>> print base64.b64decode(a)
我是字元串