From 3f23a3133701ea6ba3eba948c212ffd99a85cde8 Mon Sep 17 00:00:00 2001 From: anadepaula Date: Thu, 2 Apr 2020 23:27:09 -0300 Subject: [PATCH] First working version of project --- .gitignore | 4 ++++ CHANGELOG | 6 ++++++ README | 20 ++++++++++++++++++++ VERSION | 1 + __init__.py | 0 client.py | 25 +++++++++++++++++++++++++ localenv | 5 +++++ message_wrapper.py | 4 ++++ requirements.txt | 3 +++ server.py | 25 +++++++++++++++++++++++++ settings.py | 7 +++++++ 11 files changed, 100 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG create mode 100644 README create mode 100644 VERSION create mode 100644 __init__.py create mode 100644 client.py create mode 100644 localenv create mode 100644 message_wrapper.py create mode 100644 requirements.txt create mode 100644 server.py create mode 100644 settings.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bdb9add --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +.vscode/* +__pycache__/* +.python-version \ No newline at end of file diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..73ddaac --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,6 @@ +First working version + +- Added client and server sripts +- Added message wrapper +- Added envvar and settings handling using decouple +- Added git's "boilerplate" (reaadme, changelog, version, gitignore yadda yadda yadda) \ No newline at end of file diff --git a/README b/README new file mode 100644 index 0000000..cd96251 --- /dev/null +++ b/README @@ -0,0 +1,20 @@ +# Virtual Friendships + +An UDP-based messages service. + + +![beep](https://images.genius.com/271aa1501598f934b3a1cb3084d1e656.900x900x1.jpg) + +🎵 🎵 🎵 + +beep, beep, beep, someone's calling in the computer's network! +beep, beep, beep, I'm speking! You say "beep" and I say "hello!" + +🎵 🎵 🎵 + +# How to + +* Create a virtual environment and install the requirements listed on `requirements.txt`; +* Create a `.env` based on `localenv`; +* Run `server` and `client` scripts at the same time; +* Have fun! diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..ceab6e1 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1 \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/client.py b/client.py new file mode 100644 index 0000000..0c79767 --- /dev/null +++ b/client.py @@ -0,0 +1,25 @@ +import socket + +from message_wrapper import get_message +import settings + +udp_client_socket = socket.socket( + family=socket.AF_INET, + type=socket.SOCK_DGRAM +) + +print('UDP client up and ready to send.') + + +first_message = str.encode('You say: "beep"') +print(first_message) +udp_client_socket.sendto(first_message, settings.SERVER_ADDRESS) +first_response, _ = udp_client_socket.recvfrom(settings.BUFFER_SIZE) +print(first_response) + +while True: + message = get_message(settings.PROMPT) + udp_client_socket.sendto(message, settings.SERVER_ADDRESS) + + response_from_server, _ = udp_client_socket.recvfrom(settings.BUFFER_SIZE) + print(f'Server says: {response_from_server}') \ No newline at end of file diff --git a/localenv b/localenv new file mode 100644 index 0000000..6d9c07c --- /dev/null +++ b/localenv @@ -0,0 +1,5 @@ +SERVER_IP=127.0.0.1 +SERVER_PORT=20001 +BUFFER_SIZE=1024 + +PROMPT=You say: \ No newline at end of file diff --git a/message_wrapper.py b/message_wrapper.py new file mode 100644 index 0000000..89088ce --- /dev/null +++ b/message_wrapper.py @@ -0,0 +1,4 @@ +def get_message(prompt): + message = input(prompt) + encoded_message = str.encode(message) + return encoded_message diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8c9f8fb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +python-decouple==3.3 +ipdb==0.13.2 +ipython==7.13.0 \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 0000000..468f501 --- /dev/null +++ b/server.py @@ -0,0 +1,25 @@ +import socket + +from message_wrapper import get_message +import settings + +udp_server_socket = socket.socket( + family=socket.AF_INET, + type=socket.SOCK_DGRAM +) + +udp_server_socket.bind(settings.SERVER_ADDRESS) +print('UDP server up and listening') + +first_message, address = udp_server_socket.recvfrom(settings.BUFFER_SIZE) +print(first_message) +first_response = str.encode('I say: "hello!"') +print(first_response) +udp_server_socket.sendto(first_response, address) + +while(True): + message, address = udp_server_socket.recvfrom(settings.BUFFER_SIZE) + print(f'Client says: {message}') + + response = get_message(settings.PROMPT) + udp_server_socket.sendto(response, address) \ No newline at end of file diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..399adbe --- /dev/null +++ b/settings.py @@ -0,0 +1,7 @@ +from decouple import config + +SERVER_IP = config('SERVER_IP') +SERVER_PORT = config('SERVER_PORT', cast=int) +SERVER_ADDRESS = (SERVER_IP, SERVER_PORT) +BUFFER_SIZE=config('BUFFER_SIZE', cast=int) +PROMPT = config('PROMPT')