title: Python 3 Using Sockets over the Internet
body: <p>I have been trying to set up sockets that will work over the internet. I have search high and low through stack overflow and cannot seem to find the answer I need.</p>
  
  <p>When I run server.py I receive this error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "server.py", line 16, in &lt;module&gt;
      serversocket.bind((host, port))
  OSError: [WinError 10049] The requested address is not valid in its context
  </code></pre>
  
  <p>Note that the following seems to work over local wifi.</p>
  
  <p>The server code:</p>
  
  <pre><code># server.py 
  import socket                                      
  
  # create a socket object
  serversocket = socket.socket(
              socket.AF_INET, socket.SOCK_STREAM) 
  
  
  #get ip address
  print("You are the server.")
  print("What is your ip? If you do not know, look up &lt;my ip&gt; on google.")
  host = input("&gt;")                         
  
  port = 0                                          
  
  # bind to the port
  serversocket.bind((host, port))                                  
  
  # queue up to 5 requests
  serversocket.listen(5)   
  clientsocket,addr = serversocket.accept()                                          
  
  def messenger():
      # establish a connection    
      tm = clientsocket.recv(1024)
      tm = tm.decode('ascii')
  
      if tm == "9+10":
          message = "21"
      elif tm == "sweg":
          message = "swegedy swooty feel the booty"
      else:
          message = "feggeet"
      clientsocket.send(message.encode('ascii'))
      messenger()
  messenger()
  </code></pre>
  
  <p>The client side code:</p>
  
  <pre><code># client.py  
  import socket
  
  #get ip address
  print("You are the client.")
  print("What is your servers ip?")
  host = input("&gt;")  
  
  # create a socket object
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
  
  port = 0
  s.connect((host, port))
  
  # Receive no more than 1024 bytes
  def messenger():
      message = input("command:")
      s.send(message.encode("ascii"))
      tm = s.recv(1024)                                     
      print(tm.decode('ascii'))
      messenger()
  messenger()
  </code></pre>
  
  <p>From what I have learned, this should work as long as I insert a public IP address. However, I just get that error message.</p>
  
  <p>What am I doing wrong?</p>