Skip to content

Commit 68f1c2c

Browse files
authored
Week UWPCEWebPython#2 Assignment
Pushed to old class Git repo. Instructions not clear. Submitting it here as well.
1 parent b2f90cd commit 68f1c2c

2 files changed

Lines changed: 36 additions & 19 deletions

File tree

echo_client.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import socket
22
import sys
33

4-
54
def client(msg, log_buffer=sys.stderr):
6-
server_address = ('localhost', 10000)
5+
server_address = ('localhost', 20001)
76
# TODO: Replace the following line with your code which will instantiate
87
# a TCP socket with IPv4 Addressing, call the socket you make 'sock'
9-
sock = None
8+
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9+
sock = socket.socket()
1010
print('connecting to {0} port {1}'.format(*server_address), file=log_buffer)
1111
# TODO: connect your socket to the server here.
12-
12+
sock.connect(server_address)
1313
# you can use this variable to accumulate the entire message received back
1414
# from the server
1515
received_message = ''
@@ -19,7 +19,8 @@ def client(msg, log_buffer=sys.stderr):
1919
try:
2020
print('sending "{0}"'.format(msg), file=log_buffer)
2121
# TODO: send your message to the server here.
22-
22+
sock.sendall(msg.encode('utf-8'))
23+
# time.sleep(5)
2324
# TODO: the server should be sending you back your message as a series
2425
# of 16-byte chunks. Accumulate the chunks you get to build the
2526
# entire reply from the server. Make sure that you have received
@@ -28,15 +29,24 @@ def client(msg, log_buffer=sys.stderr):
2829
# Log each chunk you receive. Use the print statement below to
2930
# do it. This will help in debugging problems
3031
chunk = ''
31-
print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer)
32+
chunks = []
33+
bytes_recd = 0
34+
while bytes_recd < len(msg):
35+
chunk = sock.recv(2048)
36+
chunks.append(chunk)
37+
bytes_recd = bytes_recd + len(chunk)
38+
39+
received_message = b''.join(chunks).decode('utf8')
40+
print('received "{0}"'.format(received_message), file=log_buffer)
3241
finally:
3342
# TODO: after you break out of the loop receiving echoed chunks from
3443
# the server you will want to close your client socket.
3544
print('closing socket', file=log_buffer)
45+
sock.close()
3646

3747
# TODO: when all is said and done, you should return the entire reply
3848
# you received from the server as the return value of this function.
39-
49+
return received_message
4050

4151
if __name__ == '__main__':
4252
if len(sys.argv) != 2:

echo_server.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44

55
def server(log_buffer=sys.stderr):
66
# set an address for our server
7-
address = ('127.0.0.1', 10000)
7+
address = ('localhost', 20001)
88
# TODO: Replace the following line with your code which will instantiate
99
# a TCP socket with IPv4 Addressing, call the socket you make 'sock'
10-
sock = None
10+
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
11+
sock = socket.socket()
1112
# TODO: You may find that if you repeatedly run the server script it fails,
1213
# claiming that the port is already used. You can set an option on
1314
# your socket that will fix this problem. We DID NOT talk about this
1415
# in class. Find the correct option by reading the very end of the
1516
# socket library documentation:
1617
# http://docs.python.org/3/library/socket.html#example
17-
18+
#sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1819
# log that we are building a server
1920
print("making a server on {0}:{1}".format(*address), file=log_buffer)
20-
2121
# TODO: bind your new sock 'sock' to the address above and begin to listen
2222
# for incoming connections
23-
23+
sock.bind(address)
24+
sock.listen(5)
2425
try:
2526
# the outer loop controls the creation of new connection sockets. The
2627
# server will handle each incoming connection one at a time.
@@ -32,7 +33,9 @@ def server(log_buffer=sys.stderr):
3233
# the client so we can report it below. Replace the
3334
# following line with your code. It is only here to prevent
3435
# syntax errors
35-
conn, addr = ('foo', ('bar', 'baz'))
36+
# conn, addr = ('foo', ('bar', 'baz'))
37+
conn, addr = sock.accept()
38+
# conn.run()
3639
try:
3740
print('connection - {0}:{1}'.format(*addr), file=log_buffer)
3841

@@ -46,13 +49,15 @@ def server(log_buffer=sys.stderr):
4649
# a placeholder to prevent an error in string
4750
# formatting
4851
data = b''
49-
print('received "{0}"'.format(data.decode('utf8')))
52+
partial = conn.recv(16)
53+
data += partial
54+
print('Received: "{0}"'.format(data.decode('utf8')))
5055

5156
# TODO: Send the data you received back to the client, log
5257
# the fact using the print statement here. It will help in
5358
# debugging problems.
54-
print('sent "{0}"'.format(data.decode('utf8')))
55-
59+
conn.sendall(data)
60+
print('Sent: "{0}"'.format(data.decode('utf8')))
5661
# TODO: Check here to see whether you have received the end
5762
# of the message. If you have, then break from the `while True`
5863
# loop.
@@ -61,21 +66,23 @@ def server(log_buffer=sys.stderr):
6166
# message is a trick we learned in the lesson: if you don't
6267
# remember then ask your classmates or instructor for a clue.
6368
# :)
64-
69+
if len(data) == 0:
70+
break
6571
finally:
6672
# TODO: When the inner loop exits, this 'finally' clause will
6773
# be hit. Use that opportunity to close the socket you
6874
# created above when a client connected.
6975
print(
70-
'echo complete, client connection closed', file=log_buffer
76+
'Echo complete, client connection closed.', file=log_buffer
7177
)
78+
conn.close()
7279

7380
except KeyboardInterrupt:
7481
# TODO: Use the python KeyboardInterrupt exception as a signal to
7582
# close the server socket and exit from the server function.
7683
# Replace the call to `pass` below, which is only there to
7784
# prevent syntax problems
78-
pass
85+
raise
7986
print('quitting echo server', file=log_buffer)
8087

8188

0 commit comments

Comments
 (0)