Last active
August 8, 2017 15:45
-
-
Save intrd/00a39c83f752acf81775bfa9721e745a to your computer and use it in GitHub Desktop.
intrd's netcat python socket
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
## intrd's netcat python socket (v1.1) | |
# @author intrd - http://dann.com.br/ (original: http://stackoverflow.com/a/36419867) | |
# @license Creative Commons Attribution-ShareAlike 4.0 International License - http://creativecommons.org/licenses/by-sa/4.0/ | |
import socket, socks, time | |
class Netcat: | |
def __init__(self, ip, port, timeo=10, scks=False): | |
self.buff = "" | |
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.socket.settimeout(timeo) | |
if scks: | |
self.socket = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM) | |
self.socket.setproxy(PROXY_TYPE_SOCKS5, scks['host'],scks['port']) | |
self.socket.connect((ip, port)) | |
def read(self, length = 1024): | |
return self.socket.recv(length) | |
def read_until_prompt(self, length = 1024, sleep=0.1, timeo=0.5): | |
# while not data in self.buff: | |
# return self.socket.recv(length) | |
self.socket.settimeout(timeo) | |
out="" | |
while 1: | |
print ".", | |
time.sleep(sleep) | |
try: | |
buff=self.socket.recv(length) | |
#print buff #to debug all the scary shit | |
if len(buff)>=3: | |
out+=buff | |
except: | |
return out | |
def read_until(self, data): | |
while not data in self.buff: | |
self.buff += self.socket.recv(1024) | |
#print(self.buff) #enable 4 debug | |
pos = self.buff.find(data) | |
rval = self.buff[:pos + len(data)] | |
self.buff = self.buff[pos + len(data):] | |
return rval | |
def write(self, data): | |
self.socket.send(data) | |
def close(self): | |
self.socket.close() | |
## Usage | |
# nc = Netcat('ip.ip.ip.ip', port) | |
# data=nc.read_until(':') | |
# print(data) | |
# nc.write(number + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment