[Tutor] python sockets

Jon Engle jon.engle at gmail.com
Tue Jun 10 18:28:22 CEST 2014


Thank you for your help! This updated code does not "bind" the selected
port to a "listen" state, it simply exits. I feel like part of this has to
do with the creation of a procedure. Any ideas/recommendations on how to
make this loop "bind" to a socket?

    #!/usr/bin/python           # This is server.py file
    from socket import *      #import the socket library
    import thread  #import the thread library

    startingPort=input("\nPlease enter starting port: ")
    startingPort=int(startingPort)

    def setup(PORT):
 ##let's set up some constants
HOST = ''    #we are the host
PORT = startingPort    #arbitrary port not currently in use
ADDR = (HOST,PORT)    #we need a tuple for the address
BUFSIZE = 4096    #reasonably sized buffer for data

## now we create a new socket object (serv)
## see the python docs for more information on the socket types/flags
serv = socket( AF_INET,SOCK_STREAM)

##bind our socket to the address
serv.bind((ADDR))    #the double parens are to create a tuple with one
element
serv.listen(5)    #5 is the maximum number of queued connections we'll allow
print 'listening...'
conn,addr = serv.accept() #accept the connection
print '...connected!'
conn.send('TEST')
conn.close()

    for port in range (startingPort, 65535):
thread.start_new_thread(setup, (port,))
startingPort=startingPort+1
#print startingPort
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140610/40773293/attachment-0001.html>


More information about the Tutor mailing list