title: Python Sockets Peer to Peer
body: <p>I'm trying to make a simple Peer to Peer Network in Python 2.7. The problem is, I can't seem to be able to create a connection between two machines in which they both act as a server and a client. I can get it to work when one is a server and the other is a client but not when they are both, both. Do I need to create 2 sockets? Also I'm using TCP to connect.</p>
  
  <p>UPDATE:</p>
  
  <pre><code>import socket, sys               # Import socket module
  
  s = socket.socket()         # Create a socket object
  host = socket.gethostname() # Get local machine name
  port = 12345                # Reserve a port for your service.
  s.bind((host, port))        # Bind to the port
  
  if sys.argv[1] == "connect":
      host = sys.argv[2]
      s.connect((host, port))
      s.close 
  else:
      s.listen(5)                 # Now wait for client connection.
      while True:
         c, addr = s.accept()     # Establish connection with client.
         print 'Got connection from', addr
         c.send('Thank you for connecting')
         c.close()
  </code></pre>
  
  <p>The codes not very good because for someone to connect as a client they have to use the argument "connect" followed by the hostname or IP of the second machine. I can't get the two to connect and serve to each other simultaneously.</p>