Q:

Python 2.7 Socket programming ports

I had an exercise of port carousel which means that I need to build a server-client which the server asks the client for a port and then they starting to listen to the port that given, and this is the loop I got a error and I don't know how to fix it.
server:
import socket
import random

def main():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(('0.0.0.0', 1729))
    server_socket.listen(1)
    (client_socket, server_socket) = server_socket.accept()

    done = False
    while not done:
        port = client_socket.recv(4096)
        client_socket.send('i got the port' + port)
        port = int(port)

        if port != 1:
            server_socket.bind(('0.0.0.0', port))
            continue
        else:
            done = True

if __name__ == '__main__':
    main()

client:
import socket
import random

def main():
    print 'hi at anytime enter 1 to break the loop'
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect(('127.0.0.1', 1729))
    done = False

    while not done:
        port = client_socket.send(raw_input("enter port:"))
        data = client_socket.recv(4096)
        print data
        port = int(port)
        if port != 1:
            client_socket.connect(('127.0.0.1', port))
            continue
        else:
            done = True
    client_socket.close()

if __name__ == '__main__':
    main()

the error output for the server:

File "C:/Cyber/ServerFolder/ports_carrousel.py", line 18, in main
          server_socket.bind(('0.0.0.0', port))
      AttributeError: 'tuple' object has no attribute 'bind'

A:

In your main function, you do the following:
(client_socket, server_socket) = server_socket.accept()

but, server_socket.accept() actually returns two objects. The first, is a socket object, and the second one is a tuple that contains (sourceIPString, sourcePort).
Thus, by using this line of code, outlined above, you are essentially overriding the server_socket by a tuple object.
Notice that later, in line 18, you are trying to access the "bind" function of a socket, but, using a reference to a tuple object, that does not implement such a function.
What you should be doing is something along the lines of
(client_socket, client_connection_info) = server_socket.accept()

and adjust your code accordingly.

