[Tutor] python sockets

2014-06-10 Thread Jon Engle
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 124, Issue 21

2014-06-10 Thread Jon Engle
Thank you for taking the time to help me understand, here is what I am
trying to accomplish:

Client FW Server
-
1024 | Allow |   1024
1025 | Deny | 1025
1026   | Allow | 1026
65535  |  | 65535

I am trying to test the outbound configuration of a firewall (treat it like
a black box) for purposes of validating current configurations. The client
side of this is pretty straightforward but the server side is where I run
into issues. I am trying to keep this as simple as possible as I am pretty
new to Python. One of the challenges I see is not having a communications
channel between the client and the server. In the example above what
happens when port 1025 is blocked outbound and the client never receives a
response from the server? How would I make the client and the server
synchronize ports? I considered timers but again I am trying to keep this
simple. So that is what lead me to the path of binding all the ports at
once so that the client doesn't have to care what the server did/did not
receive. In my case I felt that binding all the server ports at once,was
the simplest solution but I am certainly open to other/better ways. I am
currently using python 2.7.5





On Tue, Jun 10, 2014 at 6:00 AM,  wrote:

> Send Tutor mailing list submissions to
> tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-requ...@python.org
>
> You can reach the person managing the list at
> tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>1. Re: python sockets (Alan Gauld)
>
>
> --
>
> Message: 1
> Date: Tue, 10 Jun 2014 08:35:34 +0100
> From: Alan Gauld 
> To: tutor@python.org
> Subject: Re: [Tutor] python sockets
> Message-ID: 
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 10/06/14 00:33, Jon Engle wrote:
> > I am trying to open ports 1025-65535 with the following code
>
> Why would you want to do that?
> It sounds like a great way to cripple your PC as it runs 64000 threads
> monitoring each of those ports. And it assumes that nothing else is
> using those ports already... And if you did find something trying to
> connect, what port is the server going to allocate? You've already
> grabbed them all?
>
> Can you explain your rationale for trying to do this? Unless you are
> trying a brute force technique to prevent anything from connecting to
> your computer?
>
> > 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?
>
> I think you are missing the basic concepts of server computing. You
> should never need to bind all the ports at once.
>
> However as to your code... its hard to critique because you lost the
> indentation - presumably through posting in HTML? Try using plain text
> for posting 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():
> ...
> > ## 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)
> > serv.bind((ADDR))
> > serv.listen(5)#5 is the maximum number of queued connections we'll
> allow
> >
> > serv = socket( AF_INET,SOCK_STREAM)
> > serv.bind((ADDR))
> > serv.listen(5)#5 is the maximum number of queued connections we'll
> allow
>
>
> Why do you do it twice?
>
> > print 'listening...'
>
> Is this Python 2 or 3? Your input lines above suggest its Python 3 but
> this print line suggests its Python 2. Which are you using?
>
> > PORT=PORT+1
> > conn,addr = serv.accept() #accept the connection
> > print '...connected!'
> > conn.send('TEST')
> > conn.close()
>
> You normally put the listening code inside a loop, waiting for a
> connection, processing it and then going back to listen some more
>
> > while startingPort<65535:
> > thread.start_new_thread(setup())
> > startingPort=startingPort+1
>
> Minor niggle, if you

Re: [Tutor] python sockets

2014-06-10 Thread Jon Engle
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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python sockets

2014-06-11 Thread Jon Engle
Ok, so after making the changes the code does bind the startingPort
variable but that is the only port that gets bound. Also when connecting to
the startingPort I receive the following error:

Please enter starting port: 65520

listening...

...connected!

Traceback (most recent call last):

  File "response.py", line 31, in 

thread.start_new_thread(setup(port))

TypeError: start_new_thread expected at least 2 arguments, got 1


On Tue, Jun 10, 2014 at 4:23 PM, Marc Tompkins 
wrote:

> On Tue, Jun 10, 2014 at 9:28 AM, Jon Engle  wrote:
>
>
>> for port in range (startingPort, 65535):
>>  thread.start_new_thread(setup, (port,))
>>  startingPort=startingPort+1
>> #print startingPort
>>
>
> I think you just need this:
>
> for port in range (startingPort, 65535):
>> thread.start_new_thread(setup(port))
>> #print port
>>
>
> and inside of setup, get rid of this line:
> PORT = startingPort#arbitrary port not currently in use
>
>


-- 
Cheers,

   Jon S. Engle
   jon.en...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python sockets

2014-06-11 Thread Jon Engle
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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python sockets

2014-06-11 Thread Jon Engle
Thank you for your help, this definitely gets me going in the right
direction!


On Wed, Jun 11, 2014 at 4:16 AM, Marc Tompkins 
wrote:

> On Tue, Jun 10, 2014 at 4:08 PM, Jon Engle  wrote:
> > 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.
>
> The problem is that all threads started by a program terminate when
> the program terminates - and you haven't told your program to stick
> around when it's done setting up.So it's setting up and then
> immediately exiting - and by the time you run netstat a few seconds
> later you find nothing.  (Also, by leaving HOST = '', you're listening
> at address 0.0.0.0, so good luck catching any traffic...)
> Try something like this:
>
>
> #!/usr/bin/python # This is server.py file
> from socket import *  #import the socket library
> import thread  #import the thread library
>
> def setup(PORT):
> HOST = '127.0.0.1'#we are the host
> ADDR = (HOST,PORT)#we need a tuple for the address
> BUFSIZE = 4096#reasonably sized buffer for data
>
> 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
> print '\nlistening on port %i...' % PORT
> conn,addr = serv.accept() #accept the connection
> print '\n...port %i connected!'  % PORT
> conn.send('TEST')
> conn.close()
>
> def main():
> startingPort=int(raw_input("\nPlease enter starting port: "))
> for port in range (startingPort, 65535):
> thread.start_new_thread(setup, (port,))
> quitNow = ''
> while quitNow not in ('Q', 'q'):
> quitNow = raw_input('Enter Q to quit.')
>
> if __name__ == '__main__':
> main()
>
>
> This will stick around until you enter 'Q', and if you run netstat in
> another window you'll see that it's LISTENING on all the ports you
> asked for.  (All of those print statements will show up in a
> surprising order!)
>
> I'm not running the other side of this experiment, so I haven't tested
> a successful connection... good luck.
>



-- 
Cheers,

   Jon S. Engle
   jon.en...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor