[Tutor] python sockets
Jon Engle
jon.engle at gmail.com
Wed Jun 11 01:08:42 CEST 2014
Ok, so when I run the code it immediately terminates and never 'listens' to
the ports in the loop. I have verified by running netstat -an | grep 65530
and the startingPort is not binding.
***Server***
Jons-Mac:Desktop Jon$ python response.py
Please enter starting port: 65530
Jons-Mac:Desktop Jon$
Jons-Mac:Desktop Jon$ netstat -an | grep 65530
Jons-MacDesktop Jon$
***Code***
#!/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/0a10aff6/attachment.html>
More information about the Tutor
mailing list