Skip to content

Instantly share code, notes, and snippets.

@akshithg
Created September 15, 2018 22:58
Show Gist options
  • Select an option

  • Save akshithg/892a6ea010903b7c580a0bde8f7e9897 to your computer and use it in GitHub Desktop.

Select an option

Save akshithg/892a6ea010903b7c580a0bde8f7e9897 to your computer and use it in GitHub Desktop.

Revisions

  1. akshithg created this gist Sep 15, 2018.
    60 changes: 60 additions & 0 deletions nconnect.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/usr/bin/env python

    import socket

    class NConnect:

    def __init__(self, host, port):
    self.host = host
    self.port = port
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
    self.sock.connect((host, port))
    print("[*] Connected")
    except socket.error:
    print("[!] Unable to connect")


    def read(self):
    msg = self.sock.recv(1024).decode('utf-8').strip()
    print('[>] {}'.format(msg))
    return msg


    def read_until(self, word):
    msg = ''
    while (word not in msg):
    msg += self.sock.recv(1024).decode('utf-8')

    msg = msg.strip()
    print('[>] {}'.format(msg))
    return msg


    def send(self, msg):
    print('[<] {}'.format(msg))
    self.sock.send(str(msg)+"\n")


    def close(self):
    print("[!] Closing connection")
    self.sock.close()


    host = "a.b.c.d"
    port = 8080

    nc = NConnect(host, port)

    try:
    while True:
    message = nc.read_until("//")
    print(message)

    except KeyboardInterrupt:
    print("[!] ^C Received, closing connection")
    nc.close()

    except EOFError:
    print("[!] ^D Received, closing connection")
    nc.close()