Tutorial: Socket Testing Tool Source Code

July 31, 2017 | Views: 3951

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

This time I will show you here a source code of a socket testing tool written in Python. This is a good, simple tool to test the Internet sockets using the IP of the target host and the port.

Here’s the source code:

`## Socket Testing Tool
 import sys
 import socket
 import getopt
 import threading
 import subprocess

listen = False
 command = False
 upload = False
 execute = ""
 target = ""
 upload_destination = ""
 port = 0

def usage ():
 print "Testing NET Tool"
 print "Usage: socket_testing.py -t target_host -p port"
 print "-l --listen - listen on [host]:[port] for incoming connections"
 print "-e --execute=file_to_run - execute the given file upon receiving a connection"
 print "-c --command - initialize a command shell"
 print "-u --upload=destination - upon receiving connection upload a file and write to [destination]"
 print "Examples: "
 print "socket_testing.py -t -p 10000 -l c"
 print "socket_testing.py -t -p 10000 -l -u=c:\payload.exe"
 print "socket_testing.py -t -p 10000 -l -e="cat /etc/passwd""
 print "echo 'ABCDEFGHI' | ./socket_testing.py -t -p 135"

def main():
 global listen
 global port
 global execute
 global command
 global upload_destination
 global target

if not len(sys.argv[1:]):

 opts, args = getopt.getopt(sys.argv[1:],"hle:t:p:cu:",
 except getopt.GetoptError as err:
 print str(err)

for o,a in opts:
 if o in ("-h","--help"):
 elif o in ("-l","--listen"):
 listen = True
 elif o in ("-e", "--execute"):
 execute = a
 elif o in ("-c", "--commandshell"):
 command = True
 elif o in ("-u", "--upload"):
 upload_destination = a
 elif o in ("-t", "--target"):
 target = a
 elif o in ("-p", "--port"):
 port = int(a)
 assert False,"Unhandled Option"

if not listen and len(target) and port > 0:
 buffer = sys.stdin.read()

if listen:


I hope you can learn with this tool. There are much more great examples in Python, but this is a small example. If you have questions, comment below or contact me directly.

Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
  1. I need to get all ip and subnets on network for scanning. Any methods using cmd, powershell, batch, or python recommended. is nmap the only way to go. id like to stick to the command line.

    • To scan all ip and subnets on a network you have a very popular tool called Angry IP Scanner, but this is a GUI tool. If you want a CLI (command-line tool) I recommend you pnscan, it is faster that nmap buit it does just what you want, scan for ip and subnets on a large network.

  2. Hello, I want learn python to write scripts for pentesting, how to learn python?

    • Hello, first of all, it is recommended to learn the principles of pen testing, you have to know what attack methods and how to defend. For python you have to learn the basics and the network libraries to understand how they work.

  3. Use “argparse” to manage the options

  4. Nice script. Thanks!!

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?