For my networking class we have to do an assignment using python to code a client and server that will connect to each other and the client will send these messages:
0 - Response
This message is a response to a previous request
1 - Echo Request
This is a request for the payload section to be echoed back to the client.
2 - Random Request
The server returns a random, JSON-encoded number to the client.
This is the first thing I've ever done in python, and the extent of my programming is making slot machine applets in java and using java for math(and similar things in C++, I am VERY green when it comes to programming and my brain isn't to the point where it's working in that "different way" that everyone keeps saying is the reason we have to do so much programming). I have been using online information and tutorials for my code thus far, but I am not exactly sure how I am supposed to go about getting these messages back and forth. I have tried following tutorials just to get the server to echo the client, but I feel like I am missing something big here. This is the portion of my client code to pack the header
s.connect((remote_ip , port))
version = 1
msg_type = 1
length = 4
amount_received = 0
header = struct.pack("!BBH", version, msg_type, length)
amount_expected = len(header)
while 1:
data = s.recv(size)
payload = data[4:]
s.sendall(header + payload)
print 'Received:', data
and this is the code I have for the server listening and unpacking
s.listen(1)
while True:
print 'Socket listening'
print 'waiting for connection...'
client, address = s.accept()
print '...connected from:', addr
if data:
data = client.recv(size)
print 'sending data back to client'
client.sendall('echoed', data)
else:
print 'no more data from', address
break
client.close()
The code I left out on both is just the opening sockets and connecting, I got that to work because it was pretty easy to find a tutorial, but the messages are really throwing me off and I am finding a lot of information, but it's too much and I just need a little help on what I need to focus on. I know that my code right now is pretty under developed, but if someone could point me in the right direction I would really appreciate it. Thanks in advance.