Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Sunday, 3 November 2013

A skeletal model for the client-proxy-server system

A skeletal model for the client-proxy-server system

Client

The client should do the following things:
  1. Read the file that's going to get sent
    • open(), fp.read() etc.
  2. Create a socket and establish a TCP connection to the proxy
    • socket.socket(), proxysock.connect() with SOCK_STREAM
  3. Send the file data stored into a variable in step 1 to the proxy
    • proxysock.sendall()
  4. Wait for an answer
    • proxysock.recv()
  5. Close the connection
    • proxysock.close()

Proxy

The proxy, being basically both a kind of a server and a client, should do the following things:
  1. 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()
  2. Read the data from the incoming connection
    • clientsock.recv()
  3. Store the received data into a file
    • open(), fp.write() etc.
  4. Create a socket and establish a TCP connection to the server
    • socket.socket(), serversock.connect() again with SOCK_STREAM
  5. Send the data received from the client onwards to the server
    • serversock.sendall()
  6. Wait for an aswer
    • serversock.recv()
  7. Pass the answer on to the client
    • clientsock.sendall()
  8. 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:
  1. 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()
  2. Read the data from the incoming connection
    • proxyclientsock.recv()
  3. Store the received data into a file
    • open(), fp.write() etc.
  4. Send an answer to the proxy
    • proxyclientsock.sendall()
  5. Close the connection, but leave the listening socket on
    • proxyclientsock.close()

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).

UDP Connection

TCP Connection

Friday, 11 October 2013

What is CPython

19 down vote accepted

So what is CPython

CPython is the original Python implementation. It is the implementation you download from Python.org. People call it CPython to distinguish it from other, later, Python implementations, and to distinguish the implementation of the language engine from the Python programming language itself.
The latter part is where your confusion comes from; you need to keep Python-the-language separate from whatever runs the Python code.
CPython happens to be implemented in C. That is just an implementation detail really. CPython compiles your python code into bytecode (transparently) and interprets that bytecode in a evaluation loop.

What about Jython, etc.

Jython, IronPython and PyPy are the current 'other' implementations of the Python programming language; these are implemented in Java, C# and RPython (a subset of Python), respectively. Jython compiles your Python code to Java bytecode, so your Python code can run on the JVM. IronPython lets you run Python on the Microsoft CLR. And PyPy, being implemented in (a subset of) Python, lets you run Python code faster than CPython, which rightly should blow your mind. :-)

Actually compiling to C

So CPython does itself not translate your Python code to C. It instead runs a interpreter loop. There is a project that does translate Python-ish code to C, and that is called Cython. Cython adds a few extensions to the Python language, and lets you compile your code to C extensions, code that plugs into the CPython interpreter.