Skip to content

Commit 61b1b79

Browse files
committed
Committing my echo-server assignment
1 parent 7d71474 commit 61b1b79

File tree

11 files changed

+305
-0
lines changed

11 files changed

+305
-0
lines changed

students/danwoj/Mailroom2.0/FILENAME

Whitespace-only changes.
4 KB
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
don_fname,don_mi,don_lname,don_amt,don_date
2+
Malisa,Q,Sebree,41839,18 January 2015
3+
Janella,W,Farkas,37393,27 February 2015
4+
Cordell,E,Behler,45094,2 March 2015
5+
Dannie,R,Calderone,35988,25 April 2015
6+
Sue,T,Mcnellis,27176,17 May 2015
7+
Stacee,Y,Bushard,31461,11 June 2015
8+
Dorla ,U,Perrin,10828,6 July 2015
9+
Nevada,I,Garmany,47613,6 December 2015
10+
Melba,O,Irving,24975,16 February 2016
11+
Damian,P,Warburton,17969,1 April 2016
12+
Sal,A,Hinkel,29608,11 May 2016
13+
Willetta,S,Cory,29991,21 July 2016
14+
Tennille,D,Kea,29381,23 July 2016
15+
Nichelle,F,Dampier,15561,29 July 2016
16+
Qiana,G,Pusey,48768,22 September 2016
17+
Shala,H,Vialpando,23891,23 October 2016
18+
Trina,J,Boedeker,26692,21 November 2016
19+
Earline,K,Bayer,11434,6 January 2017
20+
Mark,L,Wing,31395,17 February 2017
21+
Ezekiel,Z,Nusbaum,9944,17 August 2017
22+
Delmer,X,McAndrew,11639,28 August 2017
23+
Lecia,C,Lorenz,30423,9 October 2017
24+
Ozie,V,Avilez,49438,13 October 2017
25+
Marylynn,B,Griffy,13971,26 October 2017
26+
Daphine,N,Dillenbeck,14758,27 November 2017
27+
Noe,M,Kennell,17874,27 February 2015
28+
Florentino ,Q,McGonigal,37929,25 March 2015
29+
Lissette,W,Beekman,46658,7 May 2015
30+
Tiffanie,E,Mosser,39157,30 July 2015
31+
Marcelle,R,Chmura,48649,31 August 2015
4 KB
Binary file not shown.

students/danwoj/Mailroom2.0/donor.db

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/python
2+
3+
import sqlite3 as lite
4+
import sys
5+
6+
db_con = None
7+
db_file = 'don_list.db'
8+
9+
#try:
10+
# db_con = lite.connect(db_file)
11+
12+
# cur = db_con.cursor()
13+
# cur.execute('.schema don_list')
14+
15+
def cmd_db(sql_state):
16+
db_con = lite.connect(db_file)
17+
cur = db_con.cursor()
18+
cur.execute(sql_state)
19+
data = cur.fetchone()
20+
return data
21+
22+
# data = cur.fetchone()
23+
24+
# print(data)
25+
26+
#except lite.Error(e):
27+
28+
# print("Error %s:" % e.args[0])
29+
# sys.exit(1)
30+
31+
#finally:
32+
33+
# if db_con:
34+
# db_con.close()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3.6
2+
3+
import mailroom2, pytest
4+
5+
def test_db_query():
6+
qstate = "SELECT sql FROM sqlite_master ORDER BY tbl_name, type DESC, name"
7+
read_state = "SELECT * FROM don_list ORDER BY rowid DESC LIMIT 1;"
8+
write_state = "INSERT INTO don_list(don_fname, don_mi, don_lname, don_amt, don_date) VALUES ('Dan', 'NMN', 'Wojciechowski', 99999, '1 April 2017');"
9+
delete_state = "DELETE FROM don_list WHERE rowid = (SELECT MAX(rowid) FROM notes);"
10+
11+
result = mailroom2.cmd_db(qstate)
12+
assert result == ('CREATE TABLE don_list(\n "don_fname" TEXT,\n "don_mi" TEXT,\n "don_lname" TEXT,\n "don_amt" TEXT,\n "don_date" TEXT\n)',)
13+
14+
result = mailroom2.cmd_db(read_state)
15+
assert result == ('Marcelle', 'R', 'Chmura', '48649', '31 August 2015')
16+
17+
result2 = mailroom2.cmd_db(write_state)
18+
assert result2 == ('Dan', 'NMN', 'Wojciechowski', '99999', '1 April 2017')
19+
assert result != result2
20+
mailroom2(cmd_db(delete_state))
21+
result2 = mailroom2(cmd_db(read_state))
22+
assert result2 == result
23+
24+
#def test_add_name():
25+
# assert mailroom.add_name('Dan', 'Woj') is True
26+
27+
#def test_add_name2(capsys):
28+
# mailroom.add_name('Dan', 'Woj')
29+
# out, err = capsys.readouterr()
30+
# assert out == 'The name Dan Woj is not in the donor list. Adding them as a new donor.\n\n'
31+
32+
#def test_add_value():
33+
34+
#def test_print_letter():
35+
# assert mailroom.print_letter(4, 1234) is True
36+
37+
#def test_run_report(capfd):
38+
# mailroom.run_report()
39+
# out, err = capfd.readouterr()
40+
# assert out == '\n###################### Donor Report ######################\n\nDonor Name | Total Given | Num Gifts | Average Gift\n-----------------------------------------------------------\nBill Gates | $ 700.00 | 2 | $ 350.00\nMelvin Smith | $ 8000.00 | 3 | $ 2666.67\nDaphnie Jones | $ 2000.00 | 2 | $ 1000.00\nChauncey Doe | $ 400.00 | 1 | $ 400.00\nFrieda Whatever | $ 9000.00 | 3 | $ 3000.00\n'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
November 12,2017
2+
3+
Dear Dan,
4+
5+
Thank you so much for your generous donation of $3490.10. This contribution is so important to the work our organization does and we express our deepest appreciation of your support to our important cause.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import socket
2+
import sys
3+
4+
def client(msg, log_buffer=sys.stderr):
5+
server_address = ('localhost', 20001)
6+
# TODO: Replace the following line with your code which will instantiate
7+
# a TCP socket with IPv4 Addressing, call the socket you make 'sock'
8+
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9+
sock = socket.socket()
10+
print('connecting to {0} port {1}'.format(*server_address), file=log_buffer)
11+
# TODO: connect your socket to the server here.
12+
sock.connect(server_address)
13+
# you can use this variable to accumulate the entire message received back
14+
# from the server
15+
received_message = ''
16+
17+
# this try/finally block exists purely to allow us to close the socket
18+
# when we are finished with it
19+
try:
20+
print('sending "{0}"'.format(msg), file=log_buffer)
21+
# TODO: send your message to the server here.
22+
sock.sendall(msg.encode('utf-8'))
23+
# time.sleep(5)
24+
# TODO: the server should be sending you back your message as a series
25+
# of 16-byte chunks. Accumulate the chunks you get to build the
26+
# entire reply from the server. Make sure that you have received
27+
# the entire message and then you can break the loop.
28+
#
29+
# Log each chunk you receive. Use the print statement below to
30+
# do it. This will help in debugging problems
31+
chunk = ''
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)
41+
finally:
42+
# TODO: after you break out of the loop receiving echoed chunks from
43+
# the server you will want to close your client socket.
44+
print('closing socket', file=log_buffer)
45+
sock.close()
46+
47+
# TODO: when all is said and done, you should return the entire reply
48+
# you received from the server as the return value of this function.
49+
return received_message
50+
51+
if __name__ == '__main__':
52+
if len(sys.argv) != 2:
53+
usage = '\nusage: python echo_client.py "this is my message"\n'
54+
print(usage, file=sys.stderr)
55+
sys.exit(1)
56+
57+
msg = sys.argv[1]
58+
client(msg)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import socket
2+
import sys
3+
4+
5+
def server(log_buffer=sys.stderr):
6+
# set an address for our server
7+
address = ('localhost', 20001)
8+
# TODO: Replace the following line with your code which will instantiate
9+
# a TCP socket with IPv4 Addressing, call the socket you make 'sock'
10+
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
11+
sock = socket.socket()
12+
# TODO: You may find that if you repeatedly run the server script it fails,
13+
# claiming that the port is already used. You can set an option on
14+
# your socket that will fix this problem. We DID NOT talk about this
15+
# in class. Find the correct option by reading the very end of the
16+
# socket library documentation:
17+
# http://docs.python.org/3/library/socket.html#example
18+
#sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
19+
# log that we are building a server
20+
print("making a server on {0}:{1}".format(*address), file=log_buffer)
21+
# TODO: bind your new sock 'sock' to the address above and begin to listen
22+
# for incoming connections
23+
sock.bind(address)
24+
sock.listen(5)
25+
try:
26+
# the outer loop controls the creation of new connection sockets. The
27+
# server will handle each incoming connection one at a time.
28+
while True:
29+
print('waiting for a connection', file=log_buffer)
30+
31+
# TODO: make a new socket when a client connects, call it 'conn',
32+
# at the same time you should be able to get the address of
33+
# the client so we can report it below. Replace the
34+
# following line with your code. It is only here to prevent
35+
# syntax errors
36+
# conn, addr = ('foo', ('bar', 'baz'))
37+
conn, addr = sock.accept()
38+
# conn.run()
39+
try:
40+
print('connection - {0}:{1}'.format(*addr), file=log_buffer)
41+
42+
# the inner loop will receive messages sent by the client in
43+
# buffers. When a complete message has been received, the
44+
# loop will exit
45+
while True:
46+
# TODO: receive 16 bytes of data from the client. Store
47+
# the data you receive as 'data'. Replace the
48+
# following line with your code. It's only here as
49+
# a placeholder to prevent an error in string
50+
# formatting
51+
data = b''
52+
partial = conn.recv(16)
53+
data += partial
54+
print('Received: "{0}"'.format(data.decode('utf8')))
55+
56+
# TODO: Send the data you received back to the client, log
57+
# the fact using the print statement here. It will help in
58+
# debugging problems.
59+
conn.sendall(data)
60+
print('Sent: "{0}"'.format(data.decode('utf8')))
61+
# TODO: Check here to see whether you have received the end
62+
# of the message. If you have, then break from the `while True`
63+
# loop.
64+
#
65+
# Figuring out whether or not you have received the end of the
66+
# message is a trick we learned in the lesson: if you don't
67+
# remember then ask your classmates or instructor for a clue.
68+
# :)
69+
if len(data) == 0:
70+
break
71+
finally:
72+
# TODO: When the inner loop exits, this 'finally' clause will
73+
# be hit. Use that opportunity to close the socket you
74+
# created above when a client connected.
75+
print(
76+
'Echo complete, client connection closed.', file=log_buffer
77+
)
78+
conn.close()
79+
80+
except KeyboardInterrupt:
81+
# TODO: Use the python KeyboardInterrupt exception as a signal to
82+
# close the server socket and exit from the server function.
83+
# Replace the call to `pass` below, which is only there to
84+
# prevent syntax problems
85+
raise
86+
print('quitting echo server', file=log_buffer)
87+
88+
89+
if __name__ == '__main__':
90+
server()
91+
sys.exit(0)

0 commit comments

Comments
 (0)