patternpythonMinor
Network protocol using TCP, sending images through sockets
Viewed 0 times
protocolsendingtcpsocketsusingthroughimagesnetwork
Problem
I would like to ask about your opinion about my code. The idea is simple: I designed my own protocol, where client asks the server about the image, and the server sends the image, following the below steps (this is the actual protocol I wanted to implement):
Everything works (it seems so) but I would like to ask about what else I could possibly improve here.
client.py:
```
#!/usr/bin/env python
import socket
import sys
HOST = '127.0.0.1'
PORT = 6666
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (HOST, PORT)
sock.connect(server_address)
fname = 'fromserver.png'
try:
sock.sendall("GET\r\n")
data = sock.recv(4096)
if data:
txt = data.strip()
print '--%s--' % txt
if txt == 'OK':
sock.sendall("GET_SIZE\r\n")
data = sock.recv(4096)
if data:
txt = data.strip()
print '--%s--' % txt
if txt.startswith('SIZE'):
tmp = txt.split()
size = int(tmp[1])
print '--%s--' % size
sock.sendall("GET_IMG\r\n")
myfile = open(fname, 'wb')
amount_received = 0
while amount_received < size:
data = sock.recv(4096)
if not data :
break
amount_received += len(data)
print amount_received
txt
CLIENT SERVER
GET\r\n
----------------------------------->
OK\r\n
SIZE 1024\r\n
IMG_DATA with when image sending is over EOF\r\n
<-----------------------------------Everything works (it seems so) but I would like to ask about what else I could possibly improve here.
- Does the implementation reflects in 100% what I wanted to achieve in my protocol flow?
- Are the steps implemented as I wanted them to be in my protocol?
client.py:
```
#!/usr/bin/env python
import socket
import sys
HOST = '127.0.0.1'
PORT = 6666
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (HOST, PORT)
sock.connect(server_address)
fname = 'fromserver.png'
try:
sock.sendall("GET\r\n")
data = sock.recv(4096)
if data:
txt = data.strip()
print '--%s--' % txt
if txt == 'OK':
sock.sendall("GET_SIZE\r\n")
data = sock.recv(4096)
if data:
txt = data.strip()
print '--%s--' % txt
if txt.startswith('SIZE'):
tmp = txt.split()
size = int(tmp[1])
print '--%s--' % size
sock.sendall("GET_IMG\r\n")
myfile = open(fname, 'wb')
amount_received = 0
while amount_received < size:
data = sock.recv(4096)
if not data :
break
amount_received += len(data)
print amount_received
txt
Solution
One comment about the
Your commands are pretty short, so this is unlikely to happen unless there's some pretty bad connection issue, but still worth keeping in mind.
It is even possible that you might not detect that with the "EOF" mechanism that you implemented, because it is possible that by pure chance, the image data at the point of truncation contains the string "EOF". Then you'd have received "successfully" a corrupted image.
recv function: you should check that you indeed received the complete request/reply. For blocking sockets, recv will indeed block until some data is received, but it may return even after only 1 byte has been received. So for example in your client, you might receive something like "SIZE 1" as truncated version of "SIZE 1024\r\n". You'd have to keep recv'ing until you get to the "\r\n", for example by defining a "recvline" function that loops calling recv and appends the data into a result buffer, until "\r\n" is in the result (and perhaps stops after a maximum length is exceeded to avoid huge memory allocations if server has gone crazy :P).Your commands are pretty short, so this is unlikely to happen unless there's some pretty bad connection issue, but still worth keeping in mind.
It is even possible that you might not detect that with the "EOF" mechanism that you implemented, because it is possible that by pure chance, the image data at the point of truncation contains the string "EOF". Then you'd have received "successfully" a corrupted image.
Context
StackExchange Code Review Q#156704, answer score: 2
Revisions (0)
No revisions yet.