Use python-I to write an Interactive shell

Source: Internet
Author: User

Cabinet is a data storage service of the company. You need to add a shell client to view data and perform simple operations.

It is easier to write in python than you think. The Code is as follows:


[Python]
#! /Usr/bin/python-I
# Coding: UTF-8
 
 
Import sys
Import OS
 
 
Dirname = OS. path. dirname (sys. argv [0])
Sys. path. append (dirname + '/gen-py ')
Sys. path. append ('/usr/lib/python2.7/site-packages ')
From cabinet import CabinetStorage
From document. ttypes import Document
From thrift import Thrift
From thrift. transport import TSocket
From thrift. transport import TTransport
From thrift. protocol import TCompactProtocol
From optparse import OptionParser
 
 
# Avoid UnicodeDecodeError
Reload (sys)
Sys. setdefaultencoding ("UTF-8 ")
 
 
Try:
# Command line arguments.
Parser = OptionParser (usage = "usage: % prog [options]", version = "% prog 1.0 ")
Parser. add_option ("-c", "-- host", dest = "host", action = "store", default = "127.0.0.1", type = "string ", help = "Specify cabinet host. ")
Parser. add_option ("-p", "-- port", dest = "port", action = "store", default = 9090, type = "int ", help = "Specify cabinet port. ")
(Options, args) = parser. parse_args ()
 
 
# Connect to cabinet server.
Transport = TSocket. TSocket (options. host, options. port)
Transport = TTransport. TBufferedTransport (transport)
Protocol = TCompactProtocol. TCompactProtocol (transport)
Client = CabinetStorage. Client (protocol)
Transport. open ()
Failed t Exception as e:
Print e
OS. _ exit (1)
 
 
# Welcome info.
Print "Cabinet Shell V1.0, Copyright I-MD 2013, All rights reserved ."
Print "For usage, invoke help (). \ n"
 
 
# Main functions.
Def help ():
Print "Main functions :"
Print "get (db, id )"
Print "set (db, id, val )"
Print "delete (db, id )"
Print "unpackDocInfo (docstr )"
Print "packDocInfo (doc )"
 
 
Def get (db, id ):
Return client. Get (db, id)
 
 
Def set (db, id, val ):
Return client. Set (db, id, val)
 
 
Def delete (db, id ):
Return client. delete (db, id)
 
 
Def packDocInfo (doc ):
TransportOut = TTransport. TMemoryBuffer ()
ProtocolOut = TCompactProtocol. TCompactProtocol (transportOut)
Doc. write (protocolOut)
Return transportOut. getvalue ()
 
 
Def unpackDocInfo (docstr ):
TransportIn = TTransport. TMemoryBuffer (docstr)
ProtocolIn = TCompactProtocol. TCompactProtocol (transportIn)
Doc = Document ()
Doc. read (protocolIn)
Return doc

#! /Usr/bin/python-I
# Coding: UTF-8


Import sys
Import OS


Dirname = OS. path. dirname (sys. argv [0])
Sys. path. append (dirname + '/gen-py ')
Sys. path. append ('/usr/lib/python2.7/site-packages ')
From cabinet import CabinetStorage
From document. ttypes import Document
From thrift import Thrift
From thrift. transport import TSocket
From thrift. transport import TTransport
From thrift. protocol import TCompactProtocol
From optparse import OptionParser


# Avoid UnicodeDecodeError
Reload (sys)
Sys. setdefaultencoding ("UTF-8 ")


Try:
# Command line arguments.
Parser = OptionParser (usage = "usage: % prog [options]", version = "% prog 1.0 ")
Parser. add_option ("-c", "-- host", dest = "host", action = "store", default = "127.0.0.1", type = "string ", help = "Specify cabinet host. ")
Parser. add_option ("-p", "-- port", dest = "port", action = "store", default = 9090, type = "int ", help = "Specify cabinet port. ")
(Options, args) = parser. parse_args ()


# Connect to cabinet server.
Transport = TSocket. TSocket (options. host, options. port)
Transport = TTransport. TBufferedTransport (transport)
Protocol = TCompactProtocol. TCompactProtocol (transport)
Client = CabinetStorage. Client (protocol)
Transport. open ()
Failed t Exception as e:
Print e
OS. _ exit (1)


# Welcome info.
Print "Cabinet Shell V1.0, Copyright I-MD 2013, All rights reserved ."
Print "For usage, invoke help (). \ n"


# Main functions.
Def help ():
Print "Main functions :"
Print "get (db, id )"
Print "set (db, id, val )"
Print "delete (db, id )"
Print "unpackDocInfo (docstr )"
Print "packDocInfo (doc )"


Def get (db, id ):
Return client. Get (db, id)


Def set (db, id, val ):
Return client. Set (db, id, val)


Def delete (db, id ):
Return client. delete (db, id)


Def packDocInfo (doc ):
TransportOut = TTransport. TMemoryBuffer ()
ProtocolOut = TCompactProtocol. TCompactProtocol (transportOut)
Doc. write (protocolOut)
Return transportOut. getvalue ()


Def unpackDocInfo (docstr ):
TransportIn = TTransport. TMemoryBuffer (docstr)
ProtocolIn = TCompactProtocol. TCompactProtocol (transportIn)
Doc = Document ()
Doc. read (protocolIn)
Return doc


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.