Python server open all ports
- by user1670178
I am trying to open all ports using this code, why can I not create a loop to perform this function?
http://www.kellbot.com/2010/02/tutorial-writing-a-tcp-server-in-python/
#!/usr/bin/python # This is server.py file
##server.py
from socket import * #import the socket library
n=1025
while n<1050:
##let's set up some constants
HOST = '' #we are the host
PORT = n #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...'
n=n+1
conn,addr = serv.accept() #accept the connection
print '...connected!'
conn.send('TEST')
conn.close()
How do I make this work so that I can specify input range and have the server open all ports up to 65535?
#!/usr/bin/python # This is server.py file
from socket import * #import the socket library
startingPort=input("\nPlease enter starting port: ")
startingPort=int(startingPort)
#print startingPort
def connection():
## 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
def socketObject():
## now we create a new socket object (serv)
serv = socket( AF_INET,SOCK_STREAM)
def bind():
## bind our socket to the address
serv = socket( AF_INET,SOCK_STREAM)
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)
print 'listening...'
def accept():
conn,addr = serv.accept() #accept the connection
print '...connected!'
conn.send('TEST')
def close():
conn.close()
## Main
while startingPort<65535:
connection()
socketObject()
bind()
accept()
startingPort=startingPort+1