示例#1
0
文件: TauNet.py 项目: rhatchet/TauNet
def logMenu(i):
	print("---------Log Menu---------")
	print("What would you like to do?")
	print("1. Send Message")
	print("2. Delete Message")
	print("3. Cancel")
	print("--------------------------")

	userChoice = input(": ")
	if userChoice == "1":
		encryptedMessage = protocol.encrypt(strVersion + strFrom + strTo + '\r\n' + client.logMessage[i], PASSWORD)
		clientThread = threading.Thread(target=client.clientFunc, args=(client.logTarget[i], encryptedMessage))
		clientThread.start()
	elif userChoice == "2":
		del client.logMessage[i]
		del client.logTarget[i]
	elif userChoice == "3":
		return

	else:
		print("That is not a valid choice. Returning to Main Menu..")
示例#2
0
文件: TauNet.py 项目: rhatchet/TauNet
def MainScreen():
	print('{:-^100}'.format("Main Menu"))
	print('{:^100}'.format("What would you like to do?"))
	print('{:^100}'.format('{:<25}'.format("1. Send Message")))
	print('{:^100}'.format('{:<25}'.format("2. View Messages")))
	print('{:^100}'.format('{:<25}'.format("3. View Message from Log")))
	print('{:^100}'.format('{:<25}'.format("4. Quit")))
	print('{:-^100}'.format(""))
	userChoice = input(":")

	if userChoice == "1":
		target = addressBook()

		os.system('cls' if os.name == 'nt' else 'clear')
		message = input("Type a message:")
		while len(message.encode('utf-8')) > 934:  # restrict message to 934 bytes
			message = input("Message is too long. \nType a message:")

		encryptedMessage = protocol.encrypt(strVersion + strFrom + strTo + '\r\n' + message, PASSWORD)
		clientThread = threading.Thread(target=client.clientFunc, args=(target, encryptedMessage))
		clientThread.start()

	elif userChoice == "2":
		print("Viewing messages..")
		for msg in server.messages:
			print(msg)

	elif userChoice == "3":
		openLog()

	elif userChoice == "4":
		return False

	else:
		print("That is not a valid choice...")

	return True
示例#3
0
        def send():
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

            def close_socket():
                try:
                    sock.shutdown(socket.SHUT_RDWR)
                    sock.close()
                except socket.error:
                    pass

            try:
                sock.connect((self._ip, 9999))
            except socket.error:
                close_socket()
                raise TPLinkException.SocketError("Socket Connection Error:")

            sock.send(protocol.encrypt(data))

            buf = bytes()
            length = -1
            while True:
                chunk = sock.recv(4096)
                if not chunk:
                    break
                if length == -1:
                    length = struct.unpack(">I", chunk[0:4])[0]
                buf += chunk
                if length > 0 and len(buf) >= length + 4:
                    break

            close_socket()

            if not len(buf):
                raise TPLinkException.CorruptedDataError('No Data Received')

            return protocol.decrypt(buf[4:])
import socket
import _LOGGER
import protocol
import TPLinkException
import threading

DISCOVERY_QUERY = dict(system=dict(get_sysinfo=None),
                       emeter=dict(get_realtime=None, get_vgain_igain=None),
                       schedule=dict(get_rules=None),
                       count_down=dict(get_rules=None),
                       anti_theft=dict(get_rules=None),
                       time=dict(get_time=None, get_timezone=None),
                       cnCloud=dict(get_info=None),
                       lightingservice=dict(get_light_details=None))

DISCOVERY_QUERY = protocol.encrypt(json.dumps(DISCOVERY_QUERY))
QUERY_TARGET = "255.255.255.255"


def interface_addresses(family=socket.AF_INET):
    for fam, a, b, c, sock_addr in socket.getaddrinfo('', None):
        if family == fam:
            yield sock_addr[0]


def discover(timeout=protocol.DEFAULT_TIMEOUT, port=protocol.DEFAULT_PORT):
    """
    Sends discovery message to 255.255.255.255:9999 in order
    to detect available supported devices in the local network,
    and waits for given timeout for answers from devices.