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 module

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 marc.tompk...@gmail.com
wrote:

 On Tue, Jun 10, 2014 at 9:28 AM, Jon Engle jon.en...@gmail.com 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 Alan Gauld

On 11/06/14 00:08, Jon Engle wrote:

Ok, so when I run the code it immediately terminates and never 'listens'


This has nothing to do with your immediate problem but...


***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)



You said you were using Python 2.7.
Do not use input() in Python v2 use raw_input() instead.
Especially since you are using int() to convert the data anyway.

v2 input() evaluates whatever the user types which could
be damaging code (whether deliberate or inadvertent).
v2 input() is so dangerous it was removed from v3 and
raw_input renamed as input. (Which has just created lots
of confusion IMHO!)


 def setup(PORT):
##let's set up some constants
HOST = ''#we are the host


BTW I'm still losing nearly all indentation on your posts.
Are you posting in plain text? Nobody else is complaining
so it might just be my reader that's barfing on it...


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
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 Marc Tompkins
On Tue, Jun 10, 2014 at 4:08 PM, Jon Engle jon.en...@gmail.com 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.
___
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 Peter Otten
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.

As I've already hinted the easiest way to keep your listening threads alive 
is to use the threading instead of the thread module:

$ cat bind_ports.py
#!/usr/bin/python
import socket
import threading
import sys

HOST = ''
STARTPORT = int(sys.argv[1])
ENDPORT = int(sys.argv[2])


def setup(port):
print 'setting up port', port
addr = (HOST, port)

serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.bind(addr) 
serv.listen(1)

conn, addr = serv.accept()
print 'port', port, '...connected!'

conn.sendall('TEST')
conn.close()

print 'port', port, '...CLOSED!'

if __name__ == __main__:
for port in range(STARTPORT, ENDPORT):
threading.Thread(target=setup, args=(port,)).start()
$ python bind_ports.py 0 3 
[1] 9214
$ setting up port 0
setting up port 1
setting up port 2
netstat -an | grep 
tcp0  0 0.0.0.0:0   0.0.0.0:*   LISTEN 
tcp0  0 0.0.0.0:1   0.0.0.0:*   LISTEN 
tcp0  0 0.0.0.0:2   0.0.0.0:*   LISTEN 


To test it I've applied a minor modification to the client of the echoserver 
example in https://docs.python.org/2/library/socket.html#example

$ cat read_socket.py 
import socket
import sys

HOST = 'localhost'
PORT = int(sys.argv[1])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
$ python read_socket.py 0
port 0 ...connected!
port 0 ...CLOSED!
Received 'TEST'
$ python read_socket.py 2
port 2 ...connected!
port 2 ...CLOSED!
Received 'TEST'
$ python read_socket.py 1
port 1 ...connected!
port 1 ...CLOSED!
Received 'TEST'
$ fg
bash: fg: Programm ist beendet.
[1]+  Fertig  python bind_ports.py 0 3
$

___
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 Peter Otten
Alan Gauld wrote:

 On 11/06/14 00:08, Jon Engle wrote:
 Ok, so when I run the code it immediately terminates and never 'listens'
 
 This has nothing to do with your immediate problem but...
 
 ***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)
 
 
 You said you were using Python 2.7.
 Do not use input() in Python v2 use raw_input() instead.
 Especially since you are using int() to convert the data anyway.
 
 v2 input() evaluates whatever the user types which could
 be damaging code (whether deliberate or inadvertent).
 v2 input() is so dangerous it was removed from v3 and
 raw_input renamed as input. (Which has just created lots
 of confusion IMHO!)
 
  def setup(PORT):
 ##let's set up some constants
 HOST = ''#we are the host
 
 BTW I'm still losing nearly all indentation on your posts.
 Are you posting in plain text? Nobody else is complaining
 so it might just be my reader that's barfing on it...

No, it's not just you, I don't see any indentation either.

___
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 marc.tompk...@gmail.com
wrote:

 On Tue, Jun 10, 2014 at 4:08 PM, Jon Engle jon.en...@gmail.com 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


[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 startingPort65535:
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] python sockets

2014-06-10 Thread Lukas Nemec

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 startingPort65535:
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 startingPort65535:
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python sockets

2014-06-10 Thread Alan Gauld

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 startingPort65535:
thread.start_new_thread(setup())
startingPort=startingPort+1


Minor niggle, if you must do this use a for loop. Its tidier.
As a minimum you need some error handling to deal with
unsuccessful attempts to bind. And you need a better way
of processing connections.

But fundamentally, I suspect that whatever you are trying to
do there is a better approach!

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python sockets

2014-06-10 Thread Peter Otten
Lukas Nemec wrote:

 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 startingPort65535:
 thread.start_new_thread(setup(startingPort))
 startingPort=startingPort+1

setup() is still called in the main thread, likely listens forever which is 
why thread.start_new_thread() is never called and therefore doesn't complain 
about the missing argument...

Try

def setup(PORT):
   ... # don't reassign port inside the function

for port in range(startingPort, 65535):
thread.start_new_thread(setup, (port,))

Note that

some_func(setup(port))

passes the result of the setup() call to some_func while

some_func(setup, (port,))

passes the setup function and a 1-tuple with the port as its only item. The 
comma is necessary to create a tuple, parentheses alone have no effect:

 (1)
1
 (1,)
(1,)


PS: You should also consider using the (higlevel) threading module instead 
of the thread module.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python sockets

2014-06-10 Thread Matthew Ngaha
On Tue, Jun 10, 2014 at 5:28 PM, Jon Engle jon.en...@gmail.com wrote:

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

 def setup(PORT):
  PORT = startingPort#arbitrary port not currently in use

There's a conflict with this PORT variable.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python sockets

2014-06-10 Thread Marc Tompkins
On Tue, Jun 10, 2014 at 9:28 AM, Jon Engle jon.en...@gmail.com 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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python sockets

2014-06-10 Thread Marc Tompkins
On Tue, Jun 10, 2014 at 2:00 PM, Jon Engle jon.en...@gmail.com wrote:

 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 module

 thread.start_new_thread(setup(port))

 TypeError: start_new_thread expected at least 2 arguments, got 1


Sorry about that!  I should have read the docs
(https://docs.python.org/2/library/thread.html)
for thread.start_new_thread(); you had it right in the code you posted (it
was the rest of your loop that was a problem.)  So, change it back to:


for port in range (startingPort, 65535):
 thread.start_new_thread(setup, (port,))
 #print port


My apologies for the bum steer.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor