title: TypeError when trying to pass output of one function to another function
body: <p>I'm trying to make a chat room/text message tool with python. I need to connect to the server with the <code>.connect() method</code>. Every time I go somewhere outside of my house and connect to wifi, my private ip address changes, so I want my code to adapt to that situation by passing <code>os.system(&quot;ipconfig getifaddr &lt;wireless interface&gt;&quot;)</code> to the <code>.connect()</code> method.</p>
  <p>I found out this isn't correct after running my code, which raised a TypeError. I know this isn't correct because the <code>os.system()</code> function executed and printed my ip address to the console instead of passing that output to the function. I'm assuming that the function returned 0 to the function, indicating that nothing went wrong with the command, which is not my intention. I want the ip address to be passed to the function.</p>
  <p>It's somewhat similar to bash, like this: <code>ipconfig getifaddr &lt;wireless interface&gt; &gt; file.txt</code> where the output of the command is redirected to a file called <code>file.txt</code></p>
  <p>Here is my code:</p>
  <p><strong>client.py</strong></p>
  <pre><code>import socket
  import threading
  import subprocess
  
  nickname = input(&quot;Choose a nickname: &quot;)
  subprocess.run(&quot;./ipaddress.sh&quot;)
  
  with open(&quot;ip_addr.txt&quot;, 'r') as f:
      file = f.readline()
  
  client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  client.connect((file, 48812))
  
  def receive():
      while True:
          try:
              message = client.recv(1024).decode('ascii')
              if message == 'NICK':
                  pass
              else:
                  print(message)
  
          except:
              print(&quot;An error occurred!&quot;)
              client.close()
              break
  
  
  def write():
      while True:
          message = f'{nickname}: {input(&quot;&quot;)}'
          client.send(message.encode('ascii'))
  
  
  receive_thread = threading.Thread(target=receive)
  receive_thread.start()
  
  write_thread = threading.Thread(target=write)
  write_thread.start()
  </code></pre>
  <p>I don't know if I also have to make changes in the server file but if I do here is the code for you to see as well:</p>
  <p><strong>server.py</strong></p>
  <pre><code>import threading
  import socket
  import subprocess
  
  subprocess.run(&quot;./ipaddress.sh&quot;)
  
  with open(&quot;ip_addr.txt&quot;, 'r') as f:
      file = f.readline()
  
  host = file
  port = 48812
  
  server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server.bind((host, port))
  server.listen()
  
  clients = []
  nicknames = []
  
  def broadcast(message):
      for client in clients:
          client.send(message)
  
  def handle(client):
      while True:
          try:
              message = client.recv(1024)
              broadcast(message)
  
          except:
              index = clients.index(client)
              clients.remove(client)
              client.close()
              nickname = nicknames[index]
              broadcast(f&quot;{nickname} left the chat&quot;.encode('ascii'))
              nicknames.remove(nickname)
              break
  def receive():
      while True:
          client, address = server.accept()
          print(f&quot;Connected with{str(address)}&quot;)
  
          client.send(&quot;NICK&quot;.encode(&quot;ascii&quot;))
          nickname = client.recv(1024).decode('ascii')
          nicknames.append(nickname)
          clients.append(client)
  
          print(f&quot;Nickname of client is {nickname}\n&quot;)
          broadcast(f'{nickname}joined the chat!\n'.encode('ascii'))
          client.send(&quot;Connected to the server!\n&quot;.encode('ascii'))
  
          thread = threading.Thread(target=handle, args=(client,))
          thread.start()
  
  
  print(&quot;Server is listening...&quot;)
  receive()
  </code></pre>
  <p>And here is the full traceback:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/Users/matthewschell/PycharmProjects/TCP Chat Room/server.py&quot;, line 14, in &lt;module&gt;
      server.bind((host, port))
  socket.gaierror: [Errno 8] nodename nor servname provided, or not known
  </code></pre>
  <p><strong>EDIT:</strong> Instead of using <code>os.system()</code>, like others don't recommend, I've used subprocess instead. It fixed the exception that <code>os.system()</code> raised but now it raises another exception. See the full traceback above. Also see the edited code above. Notice the new <code>with</code> statement to open a file created by a bash script containing the ip address in both files.</p>