本文整理汇总了Python中connection.Connection.close方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.close方法的具体用法?Python Connection.close怎么用?Python Connection.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类connection.Connection
的用法示例。
在下文中一共展示了Connection.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Publisher
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
class Publisher(object):
def __init__(self, name="(publisher-only)"):
self.name = name
address = self._get_address()
try:
self.connection = Connection(address, whoiam=name)
self._log(syslog.LOG_DEBUG, "Stablished a connection with the notifier server (%s)." % esc(str(address)))
except:
self._log(syslog.LOG_ERR, "Error when creating a connection with the notifier server (%s): %s." % esc(str(address), traceback.format_exc()))
raise
self.connection.send_object(pack_message(message_type='introduce_myself', name=name))
self._safe_topics = set()
def publish(self, topic, data):
if topic not in self._safe_topics:
fail_if_topic_isnt_valid(topic, allow_empty=False)
self._safe_topics.add(topic)
#self._log(syslog.LOG_DEBUG, "Sending publication of an event with topic '%s'." % esc(topic))
self.connection.send_object(pack_message(message_type='publish', topic=topic, obj=data, dont_pack_object=False))
#self._log(syslog.LOG_DEBUG, "Publication of an event sent.")
def close(self, *args, **kargs):
self.connection.close()
def _get_address(self):
import os, ConfigParser
script_home = os.path.abspath(os.path.dirname(__file__))
parent = os.path.pardir
# TODO This shouldn't be hardcoded!
config_file = os.path.join(script_home, parent, parent, parent, "config", "publish_subscribe.cfg")
config = ConfigParser.SafeConfigParser(defaults={
'wait_on_address': "localhost",
'wait_on_port': "5555",
})
config.read([config_file])
if not config.has_section("notifier"):
config.add_section("notifier")
address = (config.get("notifier", 'wait_on_address'), config.getint("notifier", 'wait_on_port'))
return address
def __repr__(self):
return "Endpoint (%s)" % self.name
def _log(self, level, message): #TODO remove the "%" stuff over an unknow string (not local string)
header = "%s: " % esc(repr(self))
message = header + message
syslog.syslog(level, message)
return message
示例2: close
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
def close(self):
"""
Closes the connection.
Args:
None
Returns:
Nothing
"""
self.__reconnect = False
Connection.close(self)
示例3: get_library
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
def get_library(focus_name, debug):
#initialization
all_info_query = PAPER_ALL_INFO.format(focus_name)
conn = Connection()
paper_tracker = 0
result_row_count = 0
library = []
#catalog keeps track of the association: paper_id+author_id --> matrix_line
catalog = {}
conn.set_group_limit(1000000)
#try:
sql_results = conn.execute(all_info_query)
if sql_results:
for el in sql_results:
#print(el[1],el[2],el[3],el[4],el[5],el[6],el[7],el[8],el[9],el[10],el[11])
#feature cleaning
author_id = str(el[0]) #the disambiguated one for test purposes
author_name = preprocessing(el[1], stemming=False, stop_words=PREFIXES, min_word_length=0)
paper_id = el[2]
paper_title = preprocessing(el[3], stemming=True, stop_words=STOP_WORDS, min_word_length=2)
year = preprocessing(el[4], stemming=True, stop_words=STOP_WORDS, min_word_length=2)
coauthors = preprocessing(el[5], stemming=False, stop_words=PREFIXES, min_word_length=0).replace(author_name, "") #remove the author himself
subjects = preprocessing(el[6], stemming=True, stop_words=STOP_WORDS, min_word_length=2)
keywords = preprocessing(el[7], stemming=True, stop_words=STOP_WORDS, min_word_length=2)
journals = preprocessing(el[8], stemming=True, stop_words=STOP_WORDS, min_word_length=0)
institutions = preprocessing(el[9], stemming=True, stop_words=STOP_WORDS, min_word_length=2)
ref_authors = preprocessing(el[10], stemming=False, stop_words=PREFIXES, min_word_length=0)
ref_journals = preprocessing(el[11], stemming=True, stop_words=STOP_WORDS, min_word_length=2)
countries = preprocessing(el[12], stemming=True, stop_words=STOP_WORDS, min_word_length=2).replace("franc", "") #remove france
#unique identifier is paper_id concat author_id (solve the problem of 2 authors with the same name in the same paper)
unique_identifier = str(paper_id) + str(author_id)
#printout for production environment
printout = str(el[3]) + " [" + str(el[4]) + "]"
#add to library
library.insert(paper_tracker,
Paper(paper_id, paper_title, author_id, author_name, coauthors,
institutions, journals, year, subjects, keywords, ref_authors,
ref_journals, countries, unique_identifier, printout))
catalog[unique_identifier] = paper_tracker
paper_tracker = paper_tracker + 1
if debug:
print("{0} - AuthorID: {1} - PaperID: {2} - Title:{3}".format(paper_tracker, author_id, paper_id, author_name))
#close connection to db
conn.close()
return library, catalog
示例4: get_graph
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
def get_graph(library, catalog, focus_name, type):
graph_types = {
"coauthorship": COAUTHORSHIP_GRAPH_MATRIX,
"references": REFERENCES_GRAPH_MATRIX,
"subjects": SUBJECTS_GRAPH_MATRIX,
"keywords": KEYWORDS_GRAPH_MATRIX
}
if type in graph_types:
mat_graph = np.identity(len(library))
graph_query = graph_types[type].format(focus_name)
#print(graph_query)
conn = Connection()
sql_results = conn.execute(graph_query, mult=True)
if sql_results:
for el in sql_results:
unique_identifier1 = str(el[0]) + str(el[1])
unique_identifier2 = str(el[2]) + str(el[3])
mat_graph[catalog[unique_identifier1],catalog[unique_identifier2]] = el[4]
mat_graph[catalog[unique_identifier2],catalog[unique_identifier1]] = el[4]
conn.close()
return mat_graph
else:
return None
示例5: Node
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
class Node(object):
def __init__(self, syncObj, nodeAddr):
self.__syncObj = weakref.ref(syncObj)
self.__nodeAddr = nodeAddr
self.__ip = syncObj._getResolver().resolve(nodeAddr.split(':')[0])
self.__port = int(nodeAddr.split(':')[1])
self.__poller = syncObj._getPoller()
self.__conn = Connection(socket=None, timeout=syncObj._getConf().connectionTimeout)
self.__shouldConnect = syncObj._getSelfNodeAddr() > nodeAddr
self.__lastConnectAttemptTime = 0
self.__lastPingTime = 0
self.__status = NODE_STATUS.DISCONNECTED
def __del__(self):
self.__conn = None
self.__poller = None
def onPartnerConnected(self, conn):
self.__conn = conn
self.__status = NODE_STATUS.CONNECTED
self.__poller.subscribe(self.__conn.fileno(),
self.__processConnection,
POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.WRITE | POLL_EVENT_TYPE.ERROR)
def getStatus(self):
return self.__status
def isConnected(self):
return self.__status == NODE_STATUS.CONNECTED
def getAddress(self):
return self.__nodeAddr
def getSendBufferSize(self):
return self.__conn.getSendBufferSize()
def send(self, message):
if self.__status != NODE_STATUS.CONNECTED:
return False
self.__conn.send(message)
if self.__conn.isDisconnected():
self.__status = NODE_STATUS.DISCONNECTED
self.__poller.unsubscribe(self.__conn.fileno())
self.__conn.close()
return False
return True
def connectIfRequired(self):
if not self.__shouldConnect:
return
if self.__status != NODE_STATUS.DISCONNECTED:
return
if time.time() - self.__lastConnectAttemptTime < self.__syncObj()._getConf().connectionRetryTime:
return
self.__status = NODE_STATUS.CONNECTING
self.__lastConnectAttemptTime = time.time()
if not self.__conn.connect(self.__ip, self.__port):
self.__status = NODE_STATUS.DISCONNECTED
return
self.__poller.subscribe(self.__conn.fileno(),
self.__processConnection,
POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.WRITE | POLL_EVENT_TYPE.ERROR)
def __processConnection(self, descr, eventType):
if descr != self.__conn.fileno():
self.__poller.unsubscribe(descr)
return
isError = False
if eventType & POLL_EVENT_TYPE.ERROR:
isError = True
if eventType & POLL_EVENT_TYPE.READ or eventType & POLL_EVENT_TYPE.WRITE:
if self.__conn.socket().getsockopt(socket.SOL_SOCKET, socket.SO_ERROR):
isError = True
else:
if self.__status == NODE_STATUS.CONNECTING:
self.__conn.send(self.__syncObj()._getSelfNodeAddr())
self.__status = NODE_STATUS.CONNECTED
if isError or self.__conn.isDisconnected():
self.__status = NODE_STATUS.DISCONNECTED
self.__conn.close()
self.__poller.unsubscribe(descr)
return
if eventType & POLL_EVENT_TYPE.WRITE:
if self.__status == NODE_STATUS.CONNECTING:
self.__conn.send(self.__syncObj()._getSelfNodeAddr())
self.__status = NODE_STATUS.CONNECTED
self.__conn.trySendBuffer()
event = POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.ERROR
if self.__conn.getSendBufferSize() > 0:
event |= POLL_EVENT_TYPE.WRITE
if not self.__conn.isDisconnected():
self.__poller.subscribe(descr, self.__processConnection, event)
if eventType & POLL_EVENT_TYPE.READ:
if self.__conn.read():
#.........这里部分代码省略.........
示例6: Source
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
class Source(object):
def __init__(self, **kwargs):
self._socket = None
self._conf = kwargs
self._conn = None
self._tables = {}
def _query(self, sql):
"""
Query mysql with reconnecting once on failure.
"""
try:
res = []
with open_cursor(self._conn) as cursor:
cursor.execute(sql)
columns_desc = cursor.description
columns = tuple([d[0] for d in columns_desc])
for row in cursor:
res.append(dict(zip(columns, row)))
return res, columns_desc
except:
self.disconnect()
self.connect()
res = []
with open_cursor(self._conn) as cursor:
cursor.execute(sql)
columns_desc = cursor.description
columns = tuple([d[0] for d in columns_desc])
print cursor.description
for row in cursor:
res.append(dict(zip(columns, row)))
return res, columns_desc
def connect(self):
"""
build connection to mysql server.
"""
self._conn = Connection(**(self._conf))
self._conn.connect()
self._socket = self._conn.socket
def disconnect(self):
"""
disconnect mysql server.
"""
self._conn.close()
self._socket = None
def show_master_status(self):
res, _ = self._query("show master status;")
print res
return res[0]
def get_server_id(self):
return 123456
def binlog_dump(self, log_file, offset):
"""
COM_BINLOG_DUMP
+=============================================+
| packet header | packet length 0 : 3 |
| +-------------------------+
| | sequence number 3 : 1 |
+============================================+
| command packet | command code 4 : 1 | COM_BINLOG_DUMP
| +------------------------―+
| | offset 5 : 4 |
| +-------------------------+
| | flags 9 : 2 |
| +-------------------------+
| | server id 11 : 4|
| +-------------------------+
| | log name 15 : x|
+============================================+
"""
payload = ""
payload += utils.int1store(ServerCmd.BINLOG_DUMP)
payload += utils.int4store(offset)
payload += utils.int2store(0)
payload += utils.int4store(self.get_server_id())
payload += log_file
payload += "\x00"
log.debug("len(payload) = %d" % len(payload))
# send BIGLOGDUMP command and parse ok packet response.
self._socket.send(payload, 0)
ok_packet = self._socket.recv()
parser = MySQLProtocol()
ok_packet = parser.parse_ok(ok_packet)
print ok_packet
def __iter__(self):
return self
def next(self):
packet = self._socket.recv()
event = BinlogEvent(packet)
log.debug(str(event))
if event.is_eof() or event.is_error():
#.........这里部分代码省略.........
示例7: close
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
def close(self):
'''Signoff resource and close connection'''
if self._sessionId is not None:
self.signoff()
Connection.close(self)
示例8: SystemManager
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
class SystemManager(object):
"""
Lets you examine and modify schema definitions as well as get basic
information about the cluster.
This class is mainly designed to be used manually in a python shell,
not as part of a program, although it can be used that way.
All operations which modify a keyspace or column family definition
will block until the cluster reports that all nodes have accepted
the modification.
Example Usage:
.. code-block:: python
>>> from pycassa.system_manager import *
>>> sys = SystemManager('192.168.10.2:9160')
>>> sys.create_keyspace('TestKeyspace', replication_factor=1)
>>> sys.create_column_family('TestKeyspace', 'TestCF', column_type='Standard',
... comparator_type=LONG_TYPE)
>>> sys.alter_column_family('TestKeyspace', 'TestCF', key_cache_size=42, gc_grace_seconds=1000)
>>> sys.drop_keyspace('TestKeyspace')
>>> sys.close()
"""
def __init__(self, server='localhost:9160', credentials=None, framed_transport=True,
timeout=_DEFAULT_TIMEOUT):
self._conn = Connection(None, server, framed_transport, timeout, credentials)
def close(self):
""" Closes the underlying connection """
self._conn.close()
def get_keyspace_column_families(self, keyspace, use_dict_for_col_metadata=False):
"""
Returns a raw description of the keyspace, which is more useful for use
in programs than :meth:`describe_keyspace()`.
If `use_dict_for_col_metadata` is ``True``, the CfDef's column_metadata will
be stored as a dictionary where the keys are column names instead of a list.
Returns a dictionary of the form ``{column_family_name: CfDef}``
"""
if keyspace is None:
keyspace = self._keyspace
ks_def = self._conn.describe_keyspace(keyspace)
cf_defs = dict()
for cf_def in ks_def.cf_defs:
cf_defs[cf_def.name] = cf_def
if use_dict_for_col_metadata:
old_metadata = cf_def.column_metadata
new_metadata = dict()
for datum in old_metadata:
new_metadata[datum.name] = datum
cf_def.column_metadata = new_metadata
return cf_defs
def get_keyspace_description(self, *args, **kwargs):
"""
Alias for :meth:`get_keyspace_column_families()`
.. deprecated:: 1.0.4
Use :meth:`get_keyspace_column_families()`
"""
warnings.warn("SystemManager.get_keyspace_description() is deprecated; " +
"use get_keyspace_column_families instead.", DeprecationWarning)
return self.get_keyspace_column_families(*args, **kwargs)
def get_keyspace_properties(self, keyspace):
"""
Gets a keyspace's properties.
Returns a :class:`dict` with 'replication_factor', 'strategy_class',
and 'strategy_options' as keys.
"""
if keyspace is None:
keyspace = self._keyspace
ks_def = self._conn.describe_keyspace(keyspace)
return {'replication_factor': ks_def.replication_factor,
'replication_strategy': ks_def.strategy_class,
'strategy_options': ks_def.strategy_options}
def list_keyspaces(self):
""" Returns a list of all keyspace names. """
return [ks.name for ks in self._conn.describe_keyspaces()]
def describe_ring(self, keyspace):
""" Describes the Cassandra cluster """
return self._conn.describe_ring(keyspace)
def describe_cluster_name(self):
""" Gives the cluster name """
return self._conn.describe_cluster_name()
#.........这里部分代码省略.........
示例9: search_client
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
#.........这里部分代码省略.........
info[6] = self.unescape(info[6].replace("$"," ")) # Convert $ back to spaces
ip_addr = info[0]
search_query = info[6]
date_str = time.strftime("%d-%b-%Y %H:%M:%S",time.localtime())
print "\nIP:"+ip_addr+" QUERY: "+search_query
# Data storage in csv
with open('search.csv', 'a+') as output_csvfile:
fieldnames = ['date', 'ip', 'search_query']
csv_writer = csv.DictWriter(output_csvfile, fieldnames=fieldnames, delimiter=',')
# csv_writer.writeheader()
csv_writer.writerow({'date': date_str,'ip': ip_addr, 'search_query': search_query})
def configure(self,data): # Allows the users to configure the client according to his wishes.
# Load data provided by user
# if self.active(): self.debug("Cannot configure client while the connection is active.")
# if os.path.isfile(self._dir["settings"]+os.sep+self._config["savedata"]): self.load() # Load configuration if possible
if False: pass # Loading configuration causes more problems than it solves.
else:
self.debug("Configuration initiated ...")
for key in ("name","nick","host"):
if key not in data:
return self
for key in self._config.keys():
if key in data: self._config[key] = data[key]
self._config["ready"] = True
self.debug("Configuration completed successfully.")
return self
def debug(self, data):
debug_text = time.strftime("%d-%b-%Y %H:%M:%S",time.localtime())+" "+self._config["host"]+":"+str(self._config["port"])+" : "+data+"\n"
debug_mode = 1
if debug_mode==1:
# open("debug.txt","w").write(debug_text)
self._debug_fh.write(debug_text)
if debug_mode == 2:
sys.stdout.write(debug_text)
def disconnect(self): # Terminate all child threads of this object before disconnecting from the hub.
# self._debug = lambda s: sys.stdout.write(s+"\n") # NOTICE : Debugging purposes
self.debug("Terminating all searches ...")
# for item in self._search: # Terminate all searches
# if self._search[item]["socket"] is not None and self._search[item]["socket"].active():
# self._search[item]["socket"].close()
self.debug("Terminating all transfers ...")
# for transfer in self._transfer: # Terminate all transfers spawned
# if transfer["socket"].active():
# transfer["socket"].close()
self.debug("Terminating download manager thread ...")
self._download["active"] = False
# if self._download["thread"] is not None:
# self._download["thread"].join()
self.debug("Terminating step thread ...")
self._step["active"] = False
# if self._step["thread"] is not None:
# self._step["thread"].join() # Terminate step thread
# self._step["thread"] = None
self.debug("Terminating connection to server ...")
if self._socket is not None:
self._socket.close() # Terminate connection to server
self.debug("Disconnected from Hub.")
return self
def cli(self): # Provide a Command Line Interface for testing purposes before the GUI can be built
"Provides a Command Line Interface for the Direct Connect Client."
print "Command Line Interface"
while True:
try:
x = raw_input()
# if x=="!configure":
# print "Enter the Client Name : ",; name = raw_input()
# print "Enter the Hub Address : ",; host = raw_input()
# print "Enter the Nickname you wish to use : ",; nick = raw_input()
# print "Enter the Password you wish to use : ",; _pass = raw_input()
# self.configure({"name":name, "host":host, "nick":nick, "pass":_pass});
if x=="!connect": self.connect()
if x=="!disconnect": self.disconnect()
if x=="!status": print "Connection Status : "+("mode" if self._socket.active() else "Inactive")
if x=="!nicklist":
for nick in self._config["nicklist"]: print nick, self._config["nicklist"][nick]
if x=="!exit":
self.disconnect()
self._debug_fh.close()
break
# if len(x)>0 and x[0]=="?": self.search(x[1:],{"display":sys.stdout})
# if len(x)>0 and x[0]==":": self.mc_send(x[1:])
# if len(x)>0 and x[0]=="@": self.pm_send(x[1:].split()[0]," ".join(x.split()[1:]) )
# if len(x)>0 and x[0]=="~": exec (x[1:])
# if len(x)>0 and x[0]=="$": self._socket.send(x[1:])
# if len(x)>0 and x[0]=="^": self.download_tth("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
# if len(x)>0 and x[0]=="&": print [item["part"] for item in self._queue]
# if len(x)>0 and x[0]=="*":
# for t in sorted([t.name for t in threading.enumerate()]): print "THREAD :: "+t
except KeyboardInterrupt: exit()
except Exception as e: print e
return self
示例10: Connection
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
'''_
@author: Curtis Yu
@contact: [email protected]
@since: 2/17/16
'''
# For test only
from connection import Connection
if __name__ == "__main__":
con = Connection("10.66.129.97", 51000, 'TCP')
con.send_cmd('stopserver')
# con.send_cmd('startheartbeat', '10.66.4.82', '52000')
# con.send_cmd('execute', '/Applications/splunk/bin/splunk version')
con.close()
示例11: _Endpoint
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
class _Endpoint(threading.Thread):
def __init__(self, socket, notifier, codename):
threading.Thread.__init__(self)
self.connection = Connection(socket)
self.is_finished = False
self.notifier = notifier
self.codename = codename
self.name = ""
self.start()
def _is_valid_message(self, message_type, message_body):
if not message_type in ("subscribe", "publish", "unsubscribe", "introduce_myself"):
return False
return True
def _process_message(self, message_type, message_body):
if message_type == "publish":
topic, raw_obj = unpack_message_body(message_type, message_body, dont_unpack_object=True)
self.notifier.distribute_event(topic, raw_obj)
elif message_type == "unsubscribe":
topic = unpack_message_body(message_type, message_body)
self.notifier.unsubscribe_me(topic, self)
elif message_type == "introduce_myself":
self.name = unpack_message_body(message_type, message_body)
self._log(syslog.LOG_NOTICE, "Introduced himself as '%s'." % esc(self.name))
elif message_type == "subscribe":
topic = unpack_message_body(message_type, message_body)
self.notifier.register_subscriber(topic, self)
else:
self._log(syslog.LOG_ERR, "Invalid message. Unknown type: '%s'." % esc(message_type))
raise Exception("Invalid message.")
def run(self):
try:
while not self.connection.end_of_the_communication:
message_type, message_body = self.connection.receive_object()
self._process_message(message_type, message_body)
self._log(syslog.LOG_NOTICE, "The connection was closed by the other point of the connection.")
except:
self._log(syslog.LOG_ERR, "An exception has occurred when receiving/processing the messages: %s." % esc(traceback.format_exc()))
finally:
self.is_finished = True
def send_event(self, topic, obj_raw):
try:
self.connection.send_object(pack_message(message_type="publish", topic=topic, obj=obj_raw, dont_pack_object=True))
except:
self._log(syslog.LOG_ERR, "An exception when sending a message to it: %s." % esc(traceback.format_exc()))
self.is_finished = True
def close(self):
self._log(syslog.LOG_NOTICE, "Closing the connection with the endpoint.")
self.is_finished = True
self.connection.close()
self._log(syslog.LOG_NOTICE, "Connection closed")
def __repr__(self):
return "%s%s%s" % (self.codename, ((" (%s)" % self.name) if self.name else ""), (" [dead]" if self.is_finished else ""))
def _log(self, level, message):
message = ("%s: " % esc(repr(self))) + message
syslog.syslog(level, message)
示例12: SystemManager
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
class SystemManager(object):
"""
Lets you examine and modify schema definitions as well as get basic
information about the cluster.
This class is mainly designed to be used manually in a python shell,
not as part of a program, although it can be used that way.
All operations which modify a keyspace or column family definition
will block until the cluster reports that all nodes have accepted
the modification.
Example Usage:
.. code-block:: python
>>> from pycassa.system_manager import *
>>> sys = SystemManager('192.168.10.2:9160')
>>> sys.create_keyspace('TestKeyspace', replication_factor=1)
>>> sys.create_column_family('TestKeyspace', 'TestCF', column_type='Standard',
... comparator_type=LONG_TYPE)
>>> sys.alter_column_family('TestKeyspace', 'TestCF', key_cache_size=42, gc_grace_seconds=1000)
>>> sys.drop_keyspace('TestKeyspace')
>>> sys.close()
"""
def __init__(self, server='localhost:9160', credentials=None, framed_transport=True):
self._conn = Connection(None, server, framed_transport, _TIMEOUT, credentials)
def close(self):
""" Closes the underlying connection """
self._conn.close()
def get_keyspace_description(self, keyspace, use_dict_for_col_metadata=False):
"""
Returns a raw description of the keyspace, which is more useful for use
in programs than :meth:`describe_keyspace()`.
If `use_dict_for_col_metadata` is ``True``, the CfDef's column_metadata will
be stored as a dictionary where the keys are column names instead of a list.
Returns a dictionary of the form ``{column_family_name: CfDef}``
"""
if keyspace is None:
keyspace = self._keyspace
ks_def = self._conn.describe_keyspace(keyspace)
cf_defs = dict()
for cf_def in ks_def.cf_defs:
cf_defs[cf_def.name] = cf_def
if use_dict_for_col_metadata:
old_metadata = cf_def.column_metadata
new_metadata = dict()
for datum in old_metadata:
new_metadata[datum.name] = datum
cf_def.column_metadata = new_metadata
return cf_defs
def describe_keyspace(self, keyspace):
"""
Returns a human readable description of the Keyspace.
For use in a program, use :meth:`get_keyspace_description()` instead.
"""
ksdef = self._conn.describe_keyspace(keyspace)
print
spaces = " " * (35 - len('Name:'))
print "Name:", spaces, ksdef.name
print
spaces = " " * (35 - len('Replication Strategy:'))
s = ksdef.strategy_class
print "Replication Strategy:", spaces, s[s.rfind('.') + 1: ]
if ksdef.strategy_options:
spaces = " " * (35 - len('Strategy Options:'))
print "Strategy Options:", spaces, ksdef.strategy_options
spaces = " " * (35 - len('Replication Factor:'))
print "Replication Factor:", spaces, ksdef.replication_factor
print
print "Column Families:"
for cfdef in ksdef.cf_defs:
print " ", cfdef.name
print
def describe_column_family(self, keyspace, column_family):
""" Returns a human readable description of the Column Family """
try:
cfdef = self.get_keyspace_description(keyspace)[column_family]
except KeyError:
print "Column family %s does not exist in keyspace %s" % (column_family, keyspace)
return
#.........这里部分代码省略.........
示例13: len
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
if len(sys.argv) < 3:
print >> sys.stderr, 'Usage: python %s HOST PORT' % sys.argv[0]
sys.exit(1)
host = sys.argv[1]
port = int(sys.argv[2])
sock = websocket()
sock.connect((host, port))
sock.settimeout(1.0)
conn = Connection(sock)
try:
try:
while True:
msg = TextMessage(raw_input())
print 'send:', msg
conn.send(msg)
try:
print 'recv:', conn.recv()
except socket.timeout:
print 'no response'
except EOFError:
conn.close()
except SocketClosed as e:
if e.initialized:
print 'closed connection'
else:
print 'other side closed connection'
示例14: Pydis
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
class Pydis(Connection):
def __init__(self, host='127.0.0.1', port=6379, db=0, password=None):
self._conn = Connection(host, port, db, password)
self._conn.connect()
self._pre_cmd = None
self._pre_args = None
def close(self):
if self._conn:
self._conn.close()
def parse_recv(self, recv):
if isinstance(recv, bytes):
logging.debug(recv)
recv_flag, recv_body = chr(recv[0]), recv[1:]
if recv_flag == '-':
if recv_body[:5] == b'MOVED':
skipIpPort = recv_body.decode().strip().split(' ')[-1].split(':')
self._conn.goto_connect(skipIpPort[0], skipIpPort[1])
self._conn._socket.send(self._conn._pre_request);
recv = self._conn.recv()
return self.parse_recv(recv)
else:
logging.error(recv_body)
return recv_body.decode().strip()
elif recv_flag == '+':
recv_list = recv_body.decode().strip().split(CRLF)
if len(recv_list) == 1:
return recv_list[0]
return recv_list[1:]
elif recv_flag == ':':
return int(recv_body)
elif recv_flag == '$':
recv_list = recv_body.decode().strip().split(CRLF)
length_flag = recv_list[0]
if length_flag == -1:
return None
length = len(length_flag) + 5
length += int(length_flag)
while length - self._conn._socket_read_size > 0:
recv += self._conn.recv()
length -= self._conn._socket_read_size
return recv.decode().strip().split(CRLF)[1:]
elif recv_flag == '*':
while not recv.endswith(CRLF.encode()):
recv += self._conn.recv()
recv_body = recv[1:].decode().strip()
recv_list = recv_body.split(CRLF)
while len(recv_list) < 3:
new_recv = self._conn.recv()
while not new_recv.endswith(CRLF.encode()):
new_recv += self._conn.recv()
recv_list.extend(new_recv.decode().strip().split(CRLF))
value_count = recv_list[0]
res = []
i = 1
for x in range(int(value_count)):
if len(recv_list) < i + 1:
new_recv = self._conn.recv()
while not new_recv.endswith(CRLF.encode()):
new_recv += self._conn.recv()
recv_list.extend(new_recv.decode().strip().split(CRLF))
if recv_list[i] != '$-1':
if len(recv_list) < i + 2:
new_recv = self._conn.recv()
while not new_recv.endswith(CRLF.encode()):
new_recv += self._conn.recv()
recv_list.extend(new_recv.decode().strip().split(CRLF))
res.append(recv_list[i + 1])
i += 2
else:
res.append(None)
i += 1
return res
else:
logging.error('')
return recv.decode()
def set_socket_read_size(self, size):
self._conn.set_socket_read_size(size)
def pipeline(self):
return Pipeline(self._conn)
def select_db(self, db):
self._conn.send_command(SELECT, db)
recv = self._conn.recv()
return self.parse_recv(recv)
def ping(self):
self._conn.send_command(PING)
recv = self._conn.recv()
res = self.parse_recv(recv)
return res
def set(self, key, value):
self._conn.send_command(SET, key, value)
recv = self._conn.recv()
res = self.parse_recv(recv)
return res
#.........这里部分代码省略.........
示例15: main
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import close [as 别名]
def main(remote_host, recursive, file_src, file_dest, tcp_mode, disable_verify, timer, follow_links, copy_status, verbose=False, parallelism=3):
# Extract the username and hostname from the arguments,
# the ssh_port does not need to be specified, will default to 22.
username, hostname, ssh_port = Connection.unpack_remote_host(remote_host)
if verbose:
logger.setLevel(logging.DEBUG)
global gui
gui = mock.Mock()
startTime = time.time()
# Start up the user interface
gui.redraw()
# Start an ssh connection used by the xmlrpc connection,
# the comm_port is used for port forwarding.
connection = Connection(hostname=hostname, username=username, ssh_port=ssh_port)
connection.connect()
# get the rpc channel
channel = connection.channel
controller = ClientTransferController(channel, hostname, file_src, file_dest, recursive, tcp_mode, disable_verify, parallelism, follow_links, copy_status)
logger.debug("Starting transfer")
gui.log_message("Starting transfer")
start_thread = controller.start()
gui.files_processed_indicator.set_update(lambda: controller.files_processed)
gui.files_sent_indicator.set_update(lambda: controller.get_files_transfered())
start_thread.join()
gui.progress_bar.set_update(lambda: (controller.transfer_size, controller.get_server_received_size(), controller.is_transfer_validating()))
success = False
if controller.start_success:
gui.log_message("Start success.")
while not controller.is_transfer_finished():
gui.redraw()
time.sleep(0.1)
if controller.is_transfer_success():
logger.debug("Done with transfer.")
success = True
else:
logger.warn("Failed to send file.")
gui.redraw()
controller.close()
connection.close()
channel.close()
logger.debug("Closed connections.")
gui.exit()
if timer:
logger.info("Total time: " + str(time.time() - startTime))
if success:
print "Successfully transfered"
else:
print "Failed to transfer"
sys.exit()