Skip to content

Commit

Permalink
Merge pull request #3 from timmy61109/feature/v0.1.1-pytalk+timmy61109
Browse files Browse the repository at this point in the history
PyTalk

修改項目:
- 可匯入也可直接執行的主程式
- 完成核心並提供直接取用
- 加入可以使用套件、外掛、插件的功能
  • Loading branch information
timmy61109 authored Aug 28, 2019
2 parents 575c3f6 + 5b485e9 commit 1df3986
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 35 deletions.
15 changes: 10 additions & 5 deletions Module/Communication.py → Core/Communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
接受檔案傳送過來
"""
Expand All @@ -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))
34 changes: 34 additions & 0 deletions Core/Talk.py
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
Empty file added ExpandApplication/__init__.py
Empty file.
79 changes: 79 additions & 0 deletions PyTalk.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 0 additions & 16 deletions talk_client.py

This file was deleted.

14 changes: 0 additions & 14 deletions talk_server.py

This file was deleted.

0 comments on commit 1df3986

Please sign in to comment.