Codementor Events

Networking: How To Communicate Between Two Python Programs.

Published Sep 25, 2018


Python Networking

Networking is a huge field, so we’ll stick to the level concept that are important for programming. This is basically a beginner intro to networking. I am passionate about security and as I was reading and watching videos, I decided to write and share out this. 
So, networking is basically the concept of communicating across a network, be it client to client, client to server or even client to itself.

  • Client is an end device interfacing with a human
  • A server on the other hand is a device providing service to a client.

Since we already know of networking and what it really is. So we’ll go to the two main models of networking.

Models of networking

  • Client/Server Model

The first model is the client/server model. This is the most common method where mainly the client is the web browser and the server is like Google offering a web-page so that we can actually search whenever we want(the request from the client and the response from the server).

  • Peer/peer model

The other networking model is the peer to peer model which is more complex to set up and it’s more useful for software that doesn’t have to be constantly available or a more private connection. An example of this one like Google hangouts. No one wants their voice to be going to a server first before it reaches their friend. This way client works as both the server and the client thus peer to peer.

Some of the terminologies in networking that you may want to know of include addresses and ports.
>> All computers that are connected to a network, should have codes link separated by dots e.g. 127.0.0.1. This is known as an addresses and a unique identifier of a connection in the network.
  >> Ports on the other hand are the ones that come after the address and are separated by a colon from the address. Ports from 1–1024 are preserved for core protocols while the rest range from 1025–65535.

Sockets in networking are the programming abstractions for connections. They allow us to communicate in a bi-directional manner. Once connected, we can use them to send and transmit data. They implement the common transport protocols, TCP and UDP.

A socket object is created with two parameters, the AF_INET parameter which says we are going to use a standard IPV4 address or host-name, and SOCK_STREAM(for TCP clients)/SOCK_DGRAM(which is for UDP clients). We then connect the client to the server and send it some data. The last step is to receive some data back and print out the response.

This is the simplest form of a TCP Client and a TCP Server.

### TCP CLIENT
import socket

def client():
  host = socket.gethostname()  # get local machine name
  port = 8080  # Make sure it's within the > 1024 $$ <65535 range
  
  s = socket.socket()
  s.connect((host, port))
  
  message = input('-> ')
  while message != 'q':
    s.send(message.encode('utf-8'))
    data = s.recv(1024).decode('utf-8')
    print('Received from server: ' + data)
    message = input('==> ')
  s.close()

if __name__ = '__main__':
client()
## TCP SERVER
import socket

def server():
  host = socket.gethostname()   # get local machine name
  port = 8080  # Make sure it's within the > 1024 $$ <65535 range
  
  s = socket.socket()
  s.bind((host, port))
  
  s.listen(1)
  client_socket, adress = s.accept()
  print("Connection from: " + str(addr))
  while True:
    data = c.recv(1024).decode('utf-8')
    if not data:
      break
    print('From online user: ' + data)
    data = data.upper()
    c.send(data.encode('utf-8'))
  c.close()

if __name__ = '__main__':
server()

If you are a pen-tester and work in the confines of large enterprise environments there have been countless times during penetration tests that you’ve needed to whip up a TCP client to test for services, send garbage data, fuzz, or any number of other tasks. Learning to write your own scripts and tool substitutes can really come in handy just as a public toilet in the streets.

TCP connections uses a reliable connection based protocol. A connection is formed between two devices and keeps it going until it is closed. If a piece of data is lost in it’s way through the internet, the protocol organizes the data to be resent. This is slower than other protocols due to multiple checking and therefore used in programs that must have all the data arrive such as a web browser.

UDP

This stands for User Datagram Protocol which is an un reliable connection- less based protocol. This means that it never actually ensures connection with the other device, it just sends data to an address whether there is a connection or not. This is unreliable since we are never sure whether the data will be received or not, if it’s lost that’s forever. This however makes it very fast since it doesn’t resend or keeps checking on the data all the time. Mainly useful for software like Online video games and streaming.

Hope I helped someone out or helped someone pass some time while reading this. If so, leave a comment on what you think of this or post any questions you have.

Discover and read more posts from Krafty Coder
get started
post commentsBe the first to share your opinion
Show more replies