本文整理汇总了Python中Client.Client.send_message方法的典型用法代码示例。如果您正苦于以下问题:Python Client.send_message方法的具体用法?Python Client.send_message怎么用?Python Client.send_message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Client.Client
的用法示例。
在下文中一共展示了Client.send_message方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from Client import Client [as 别名]
# 或者: from Client.Client import send_message [as 别名]
class Eve:
def __init__(self):
self.Alice = Client()
self.Alice.open_connection()
self.Bob = Client()
self.Bob.open_connection()
self.Eve = Client()
self.Eve.open_connection()
self.alice_stolen_password = ""
def wait_for_alice(self):
print "Binding..."
self.Eve.sock.bind((HOST, LISTEN_PORT))
print "Listening..."
self.Eve.sock.listen(10)
print "Accepting..."
conn, address_info = self.Eve.sock.accept()
print 'Connected with ' + address_info[0] + ':' + str(address_info[1]), "\n===================================="
th = Thread(target=self.start_communication_with_alice, args=(conn,))
th.start()
# ===== Pretending to be Alice =====
def say_hello_to_bob(self):
print "Waving to Bob"
self.Bob.send_message(self.Bob.sock, "Hey I'm Alice")
bob_response = self.Bob.read_plain_message(self.Bob.sock)
print "Bob returns with ", bob_response
return bob_response
# ===== Pretending to be Bob =====
def start_communication_with_alice(self, conn):
'''
:param conn: alice's socket
:return:
'''
print "Alice Connected me, lets connect Bob..."
self.Bob.sock.connect((DST_IP, CONNECTION_PORT))
print "Connected with Bob"
while True:
print "==============================="
data = conn.recv(1024)
if not data:
conn.close()
return
if data == "Hey I'm Alice":
print "Alice Waved Me, lets wave bob..."
bob_response = self.say_hello_to_bob()
self.Alice.send_message(conn, bob_response)
print "Waved to Alice"
elif '-----BEGIN PUBLIC KEY-----' in data:
print "Alice sent her public key\nStoring..."
self.Alice.rsa.dst_public_key = RSA.importKey(str(data))
print "Sending Bob Eve's public key"
self.Eve.send_public_key(self.Bob.sock)
print "Sent bob EVE's public key"
elif "QUIT" == data:
self.Eve.send_message(self.Bob.sock, "QUIT")
self.Bob.sock.close()
conn.close()
print "Eve is quitting..."
break
elif data == "GET PUBLIC KEY":
print "Getting Public key from Bob..."
self.Eve.send_message(self.Bob.sock, "GET PUBLIC KEY")
self.Bob.read_public_key(self.Bob.sock)
print "Sending Alice our public key..."
self.Eve.send_public_key(conn)
print "Done"
elif self.Eve.read_enc_message(data) == "GET PWD":
print "Alice has requested for password, lets ask Bob for it first..."
self.Bob.send_enc_message(self.Bob.sock, "GET PWD")
print "Waiting for Bob's response..."
self.alice_stolen_password = self.Eve.read_enc_message(self.Bob.sock.recv(1024))
print "I've got the password from Bob: " + self.alice_stolen_password
# print "Lets send Alice's a changed version of the password"
# changed_version_of_password = raw_input("Type in a password to foll Alice")
# self.Alice.send_enc_message(conn, changed_version_of_password)
self.Alice.send_enc_message(conn, self.alice_stolen_password)
elif self.Eve.read_enc_message(data) == "SET PWD":
print "Alice has requested to change password, lets change the password"
self.Bob.send_enc_message(self.Bob.sock, "SET PWD")
if self.Bob.read_plain_message(self.Bob.sock) == "OK":
self.Alice.send_message(conn, "OK")
self.alice_stolen_password = self.Eve.read_enc_message(conn.recv(1024))
print "Alice changed her password to " + self.alice_stolen_password
print "Sending Bob alice's new password"
self.Bob.send_enc_message(self.Bob.sock, self.alice_stolen_password)
#.........这里部分代码省略.........
示例2: Client
# 需要导入模块: from Client import Client [as 别名]
# 或者: from Client.Client import send_message [as 别名]
from Client import Client
clientA= Client()
clientA.connect()
while True:
user_input=input("Type message")
clientA.send_message(user_input)
print(clientA.recv_message())
示例3: ChatInterface
# 需要导入模块: from Client import Client [as 别名]
# 或者: from Client.Client import send_message [as 别名]
class ChatInterface(object):
def __init__(self, host, port, username):
'''
Sets up and initializes
'''
self.host = host
self.port = port
self.username = username
self.running = True
self.client = Client(self)
self.client.start(self.host, int(self.port))
self.lock = Lock()
self.NR_OF_MESSAGES_TO_DISPLAY = 15
self.stdscr = None
curses.wrapper(self.main)
def main(self, stdscr):
self.stdscr = stdscr
curses.echo()
self.client.login(self.username)
time.sleep(1) #delay, in case of poor connection
self.main_loop(stdscr)
def main_loop(self, stdscr):
while self.running:
if self.client.logged_in:
self.stdscr.clear()
with self.lock:
self.display_messages()
message = self.stdscr.getstr()
self.parse(message)
self.stdscr.refresh()
else:
self.stdscr.clear()
for i in xrange(len(self.client.messages)):
self.stdscr.addstr(i, 0, self.client.messages[i])
self.stdscr.addstr(self.NR_OF_MESSAGES_TO_DISPLAY, 0, '--> Please enter username to log in:')
self.stdscr.move(self.NR_OF_MESSAGES_TO_DISPLAY + 1, 0)
username = self.stdscr.getstr()
self.client.login(username)
self.stdscr.refresh()
time.sleep(1)
def display_messages(self):
if self.client.messages:
if len(self.client.messages) < self.NR_OF_MESSAGES_TO_DISPLAY:
for i in xrange(len(self.client.messages)):
self.stdscr.addstr(i, 0, self.client.messages[i])
else:
for i in xrange(self.NR_OF_MESSAGES_TO_DISPLAY):
self.stdscr.addstr(i, 0, self.client.messages[len(self.client.messages) - (self.NR_OF_MESSAGES_TO_DISPLAY) + i])
self.stdscr.addstr(self.NR_OF_MESSAGES_TO_DISPLAY, 0, '--> Enter a message:')
self.stdscr.move(self.NR_OF_MESSAGES_TO_DISPLAY + 1, 0)
self.stdscr.refresh()
def parse(self, message):
if message == 'logout':
self.client.logout()
else:
self.client.send_message(message)
示例4: Client
# 需要导入模块: from Client import Client [as 别名]
# 或者: from Client.Client import send_message [as 别名]
from Client import Client
from sys import stdin
from Const import Const
client = Client(Const.SERVER_IP, Const.SERVER_PORT)
client.connect()
client.create_listen_thread()
print("Chat started: ")
while True:
msg = stdin.readline(Const.SERVER_PORT)
client.send_message(msg)