[Tutor] python sockets
Lukas Nemec
lu.nemec at gmail.com
Tue Jun 10 09:28:45 CEST 2014
Hi,
fist - are you really triyng to have open 64 000 ports? ok, i suppose
you have your reasons, but this is not a good idea - you'll block most
applications that use these ports ..
The problem is with your main function -
you have PORT defined, but it is not global, it is only local, and when
you add +1 to it, next spawned process will have PORT with previous value.
either use global PORT, or have it as a parameter for the function:
main(port):
......
PORT = port
while startingPort<65535:
thread.start_new_thread(setup(startingPort))
startingPort=startingPort+1
Lukas
On 06/10/2014 01:33 AM, Jon Engle wrote:
> I am trying to open ports 1025-65535 with the following code (Mostly
> found online with small modifications). I am unable to "bind" anything
> other than the one port which is selected as input. What am I missing
> and how do I bind all the ports simultaneously?
>
> #!/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():
> ##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
>
> 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...'
> PORT=PORT+1
> conn,addr = serv.accept() #accept the connection
> print '...connected!'
> conn.send('TEST')
> conn.close()
>
> while startingPort<65535:
> thread.start_new_thread(setup())
> startingPort=startingPort+1
>
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140610/717a1164/attachment-0001.html>
More information about the Tutor
mailing list