Skip to content
Permalink
Browse files
Echo Server Code updated
  • Loading branch information
aa9863 committed Oct 18, 2020
1 parent f01eed4 commit 724034da5980c1790c59996856d4adecfdea89b4
Showing 1 changed file with 169 additions and 2 deletions.
@@ -17,7 +17,7 @@ In the **Buggs** directory you can find starting code for our simple Whatsapp cl
- The **main.py** file, is the Entry Point. I dont really expect you to modify this.
HOWEVER, you can change the logging level to remove debugging messages
- The **network** file contains the base networking code. You can Ignore this.
- The **driver.py** file is what you need to edit.
- The twho **<whatever>Driver.py** file is what you need to edit.

### Driver Code

@@ -131,7 +131,174 @@ Capture the traffic again and see if you can identify the messages.
We can filter this using the filter ```tcp.port == 4242```
##
## Part 2: Changing the Behaviour of the Client
Before we get adding encryption lets look at changing the behaviour of the chat
The two *driver* files contain the logic for sending and recieving data.
### Changing the Send Logic.
To change what the Client sends we can need to modify the *sendMessage* function in *clientDriver*
```python
def sendMessage(self, message):
"""Helper Function to send a message
This is what we need to modify to send a message
"""
self.sock.sendall(message.encode())
```
Lets change the Code to Prefix Everything with "MESSAGE"
!!! note "Keeping the Code Sane"
We could do this in-place, but to keep the code sane we are going to create a
a new funciton to modify the data.
This means we can just change the function, rahter than have to fix the
send message code each time we make a modifiaction
```python
def modifyData(self, message):
procMessage = "MESSAGE: {0}".format(message)
return procMessage
def sendMessage(self, message):
"""Helper Function to send a message
This is what we need to modify to send a message
"""
procMesage = modifyData(message)
self.sock.sendall(procMessage.encode())
```
!!! task "Change the Send Code"
Lets pretend we are sending a password
Change the modifyData function to hash the input and send it
### Changing the Server Functionality
We can also modify the **procesData()** function in **serverDriver**, to change the behaviour
when we receive data.
For example. We could print an all uppercase version of the Input using the code below
```
def processData(self, data):
"""
And process the data
Add our Logic Here
"""
self.log.debug("Message {0}".format(data))
self.log.debug("Uppercase {0}".format(data.upper())
```
!!! task "Change the Receive Code"
Lets continue the password task.
Change the receive code to take the hash that you have sent
in the task above. And check it against a known hash (ie the correct password)
Print a message if the login is successful
### Getting the Server to return Data.
!!! note
The output may be a bit confusing when you are running locally, as you are sending
a message back to yourself (thus everything is in one window)
Rememebr you can also send to anopther machine
Finally lets get the server to send a message back to the client. I am going to Echo back the message in uppercase
We need to do two things here.
#### 1. Tell the client to expect data to come back
After our send call we can do another recv() to tell the client to expect data from the server
```
def sendMessage(self, message):
"""Helper Function to send a message
This is what we need to modify to send a message
"""
self.sock.sendall(procMessage.encode())
#And Expect a response
data = self.sock.recv(1024)
print("RESONSE FROM SERVER {0}".format(data))
```
#### 2. Tell the Server to send something
The server can use the ```self.request.sendall()``` function to send data back.
```
def processData(self, data):
"""
And process the data
Add our Logic Here
"""
self.log.debug("Message {0}".format(data))
#Send something back
self.request.sendall(data.upper())
```
You can see that the server will now send the data back in upper case
```
$ python main.py
WARNING:root:YOU ARE NOT EXPECTED TO IMPORT THIS CODE
WARNING:root:Run as $python main.py
You need to specify an address to connect to
for example:
- 127.0.0.1 (for your machine)
- 10.0.2.15 (for someone elses machine)
>127.0.0.1
DEBUG:MAIN:Creating Server
DEBUG:CLIENT:Initialise Client
>DEBUG:SERVER:Connection from ('127.0.0.1', 52314)
Foo
DEBUG:SERVER:Message b'Foo'
RESONSE FROM SERVER b'FOO'
>Bar
DEBUG:SERVER:Message b'Bar'
RESONSE FROM SERVER b'BAR'
>
```
!!! task "Login Client"
Modify the hash sending and checking code you developed eariler
to simulate a login process.
Get the server to send a message back to the client if login was
successfull

0 comments on commit 724034d

Please sign in to comment.