diff --git a/Module/Communication.py b/Core/Communication.py similarity index 67% rename from Module/Communication.py rename to Core/Communication.py index 92dd3fa..72fd559 100644 --- a/Module/Communication.py +++ b/Core/Communication.py @@ -8,7 +8,8 @@ class tcp(): def __init__(self): self.init_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - def server(self, Host="0.0.0.0", Port=5000): + def server(self, Host="0.0.0.0", + Port=5000, Charset="utf8", end_of_message=True): """ 接受檔案傳送過來 """ @@ -21,12 +22,16 @@ def server(self, Host="0.0.0.0", Port=5000): with conn: while True: data = conn.recv(4096) - if not data: + decode_data = data.decode(Charset) + if not data or decode_data == "None": break else: - yield data.decode('utf8') + yield decode_data + if end_of_message and decode_data == "None": + break - def client(self, Host="127.0.0.1", Port=5000, message="test"): + def client(self, Host="127.0.0.1", Port=5000, message="test", + Charset="utf8"): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client: client.connect((Host, Port)) - client.sendall(message.encode('utf8')) + client.sendall(message.encode(Charset)) diff --git a/Core/Talk.py b/Core/Talk.py new file mode 100644 index 0000000..cc74f5c --- /dev/null +++ b/Core/Talk.py @@ -0,0 +1,34 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# coding = UTF-8 +from .Communication import tcp + +tcp = tcp() + + +class talk(): + def __init__(self): + self.version = "0.0.1" + + def accept_message(self, host="0.0.0.0", port=5000, charset="utf8", + end_of_message=True): + data = tcp.server(Host=host, Port=port, + Charset=charset, end_of_message=end_of_message) + while True: + message = next(data) + if message == "None": + yield None + break + else: + yield message + + def send_message(self, host="127.0.0.1", port=5000, charset="utf8"): + while True: + message = str(input("You:")) + tcp.client(Host=host, Port=port, + message=message, Charset=charset) + if message == "None": + yield None + break + else: + yield True diff --git a/Module/__init__.py b/Core/__init__.py similarity index 100% rename from Module/__init__.py rename to Core/__init__.py diff --git a/Module/File.py b/ExpandApplication/File.py similarity index 100% rename from Module/File.py rename to ExpandApplication/File.py diff --git a/ExpandApplication/__init__.py b/ExpandApplication/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/PyTalk.py b/PyTalk.py new file mode 100644 index 0000000..dd20108 --- /dev/null +++ b/PyTalk.py @@ -0,0 +1,79 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# coding = UTF-8 +from Core.Talk import talk +from argparse import ArgumentParser + +talk = talk() + + +class pytalk(): + def __init__(self, mode="client", + host="127.0.0.1", + port=5000, + charset="utf8", + end_of_message=True): + self.mode = mode + if mode != "client" and mode != "server": + raise ValueError('Not is client or server') + + self.accept_message = talk.accept_message( + host=host, port=port, charset=charset, + end_of_message=end_of_message) + + self.send_message = talk.send_message(host=host, + port=port, + charset=charset) + + def accept(self): + while True: + print(next(self.accept_message)) + + def send(self): + while True: + next(self.send_message) + + @property + def runtalk(self): + if self.mode == "client": + self.send() + elif self.mode == "server": + self.accept() + + +if __name__ == '__main__': + parser = ArgumentParser() + + parser.add_argument('-m', '--mode', default='client', + type=str, + help="Use mode server or client, default is client", + dest="mode") + + parser.add_argument('-s', '--server', default='127.0.0.1', + type=str, + help="Host name or IP address of your server.", + dest="host") + + parser.add_argument('-p', '--port', default=2000, + type=int, + help='Port number of your server.', + dest="port") + + parser.add_argument( + '--end_of_message', default=True, + type=bool, + help='When the message is the end command, used for the server.', + dest='end_of_message' + ) + + parser.add_argument('--charset', default="utf8", + type=str, + help='Encoding format.', + dest='charset' + ) + + args = parser.parse_args() + + pytalk = pytalk(mode=args.mode, host=args.host, port=args.port, + charset=args.charset, end_of_message=args.end_of_message) + pytalk.runtalk diff --git a/talk_client.py b/talk_client.py deleted file mode 100644 index 14b4f71..0000000 --- a/talk_client.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- -# coding = UTF-8 -from Module.Communication import tcp - - -tcp = tcp() -server_host = str(input("Server Host:")) -server_port = int(input("Server Port:")) -while True: - message = str(input("Key Your Message:")) - if message == "None": - print("exit") - else: - tcp.client(Host=server_host, Port=server_port, message=message) - print(message) diff --git a/talk_server.py b/talk_server.py deleted file mode 100644 index 82ff268..0000000 --- a/talk_server.py +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- -# coding = UTF-8 -from Module.Communication import tcp - -tcp = tcp() -server_host, server_port = "0.0.0.0", 5000 -data = tcp.server(Host=server_host, Port=server_port) -while True: - message = next(data) - if message == "None": - break - else: - print(message)