Skip to content

Instantly share code, notes, and snippets.

@akshithg
Created September 15, 2018 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshithg/892a6ea010903b7c580a0bde8f7e9897 to your computer and use it in GitHub Desktop.
Save akshithg/892a6ea010903b7c580a0bde8f7e9897 to your computer and use it in GitHub Desktop.
python nc
#!/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment