A skeletal model for the client-proxy-server system
Client
The client should do the following things:
- Read the file that's going to get sent
- Create a socket and establish a TCP connection to the proxy
socket.socket() , proxysock.connect() with SOCK_STREAM
- Send the file data stored into a variable in step 1 to the proxy
- Wait for an answer
- Close the connection
Proxy
The proxy, being basically both a kind of a server and a client, should do the following things:
- Create a listening socket, wait for incoming connections from the client, accept and store the handle of an incoming connection
socket.socket() , proxyserversock.bind() , proxyserversock.listen() , proxyserversock.accept()
- Read the data from the incoming connection
- Store the received data into a file
- Create a socket and establish a TCP connection to the server
socket.socket() , serversock.connect() again with SOCK_STREAM
- Send the data received from the client onwards to the server
- Wait for an aswer
- Pass the answer on to the client
- Close the connections to the server and the client, but leave the listening socket on
serversock.close() , clientsock.close()
Server
The server should do the following things:
- Create a listening socket, wait for incoming connections from the proxy, accept and store the handle of an incoming connection
socket.socket() , serversock.bind() , serversock.listen() , serversock.accept()
- Read the data from the incoming connection
- Store the received data into a file
- Send an answer to the proxy
proxyclientsock.sendall()
- Close the connection, but leave the listening socket on
Additional notes
After implementing the basic model described above, you can easily
start adding more features. For example, you could add a sleep in the
proxy between steps 3 and 4 and instead of directly sending the data
received from the client to the server, you could read the file and send
the contents - this way you can manually change the file before it gets
sent to the server.
I also suggest you read Beej's Guide to Network Programming
if you want to really learn the basics of network programming. The
examples are in C, but the basic principles also apply in Python (and
other languages as well). |
|
No comments:
Post a Comment