How to Code a Basic TCP/IP Client & Server Duo in Python « Null Byte :: WonderHowTo

TCP/IP Client / Server ApplicationHello! Today's blog post will be about coding a very rudimentary Client and Server application duo, which will communicate by using TCP Sockets.It is, as illustrated, in principle, just two cans and a string that ties them together. The server will listen for clients and accept incoming data, while the client will, upon startup, check for the server on a predefined port and if found, will enable you to send messages! If not, it will simply exit with an exception.For a more substantial read about TCP/IP, refer tothis Wiki article. It should be sufficient and cover the basics of the model.RequirementsPythonStep1Server ApplicationThis is going to be our receiving end (no puns, please) of the duet.#!C:\python27 #Replace with usr/bin/ clause for Linuximport SocketServer #imports the socket server libraryclass EchoHandler( SocketServer.StreamRequestHandler ): #declares the classdef handle(self): #installs handlerinput= self.request.recv(1024) #defines request bufferprint "Input: %r" % ( input, ) #prints data receivedself.request.send("Received: %r" % ( input, ) ) #sends message to client saying..#.."message received"server= SocketServer.TCPServer( ("",7000), EchoHandler )#adds listener at port 7000print "Starting Server..."server.serve_forever() #enables the server to run forever as long as a client is connectedSimple enough, right?Step2Client ApplicationNow we will code the application which will enable us to send messages to the server.#!C:\python27import socketclass Client( object ): #defines client classrbufsize= -1 #sets read bufferwbufsize= 0 #sets write bufferdef __init__( self, address=('localhost',7000) ): #defines initialisation functionself.server=socket.socket( socket.AF_INET, socket.SOCK_STREAM )#initiates socket by passing address and protocol parametersself.server.connect( address ) # connects to set address and portself.rfile = self.server.makefile('rb', self.rbufsize) #makes file object for response processingself.wfile = self.server.makefile('wb', self.wbufsize) # same as above but for writing/response sendingdef makeRequest( self, text ): # request function which will handle the data transferself.wfile.write( text )data= self.rfile.read()self.server.close() # closes this instance of the connectionreturn dataprint "Connecting to Echo Server"i = 0 #from here to end of file, infinite loop for message sending to serverwhile (i > -1):c= Client()response = c.makeRequest(raw_input("Enter something: "))print repr(response)print "Finished"And that's it.End NotesFire up the "Server" app first, then launch the client and try sending some data.If all goes well, you should get the server to print out some received data. Hope you enjoy making these apps!Want to start making money as a white hat hacker?Jump-start your hacking career with our2020 Premium Ethical Hacking Certification Training Bundlefrom the newNull Byte Shopand get over 60 hours of training from cybersecurity professionals.Buy Now (90% off) >Other worthwhile deals to check out:97% off The Ultimate 2021 White Hat Hacker Certification Bundle99% off The 2021 All-in-One Data Scientist Mega Bundle98% off The 2021 Premium Learn To Code Certification Bundle62% off MindMaster Mind Mapping Software: Perpetual LicenseRelatedHow To:Reverse Shell Using PythonHow To:Program Your Own Little RAT (Part 1) Getting the Server WorkingHow To:Program Your Own Little RAT (Part 2) Getting the Client WorkingHacker Fundamentals:A Tale of Two StandardsHow To:Create Packets from Scratch with Scapy for Scanning & DoSingHow To:Advanced System Attacks - Total GuideHacking macOS:How to Create an Undetectable PayloadHow To:Create a Basic Client-Server Connection in C Part-1Hacking macOS:How to Create a Fake PDF Trojan with AppleScript, Part 1 (Creating the Stager)Hack Like a Pro:Networking Basics for the Aspiring Hacker, Part 2 (TCP/IP)How To:Make a Change-of-IP Notifier in PythonCommunity Byte:Coding an IRC Bot in Python (For Beginners)How To:Create a Reverse Shell to Remotely Execute Root Commands Over Any Open Port Using NetCat or BASHHow To:Make a Gmail Notifier in PythonCommunity Byte:Coding a Web-Based Password Cracker in PythonNews:8 Wireshark Filters Every Wiretapper Uses to Spy on Web Conversations and Surfing HabitsNews:Learning Python 3.x as I go (Last Updated 6/72012)Community Byte:HackThisSite Walkthrough, Part 8 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 7 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 3 - Legal Hacker TrainingHow To:Create an SSH Tunnel Server and Client in LinuxMastering Security, Part 2:How to Create a Home VPN TunnelWeekend Homework:How to Become a Null Byte Contributor (2/17/2012)How To:Things to Do on WonderHowTo (01/18 - 01/24)How To:Chain Proxies to Mask Your IP Address and Remain Anonymous on the WebNews:Anonymity, Darknets and Staying Out of Federal Custody, Part Four: The Invisible InternetCommunity Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingUDP Flooding:How to Kick a Local User Off the NetworkCommunity Byte:HackThisSite, Realistic 5 - Real Hacking SimulationsHow To:Send SMS Messages with PythonPygame:All You Need to Start Making Games in PythonHow To:Enable Code Syntax Highlighting for Python in the Nano Text EditorCommunity Byte:HackThisSite, Realistic 4 - Real Hacking SimulationsCommunity Byte:HackThisSite, Realistic 3 - Real Hacking SimulationsCommunity Byte:HackThisSite, Realistic 2 - Real Hacking SimulationsGoodnight Byte:Coding an IRC Bot in Python (For Beginners)How To:Run an FTP Server from Home with Linux