Created
September 15, 2018 22:58
-
-
Save akshithg/892a6ea010903b7c580a0bde8f7e9897 to your computer and use it in GitHub Desktop.
python nc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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