Newbie: Keep TCP socket open
s0suk3 at gmail.com
s0suk3 at gmail.com
Mon May 19 11:48:42 EDT 2008
On May 19, 10:25 am, "Alan Wright" <alan.wri... at volubill.com> wrote:
> Hi Folks,
> I am newbie to Python, but have successfully created a simple client and
> server setup, I have one issue though.
>
> I am trying to test a box by sending many TCP conns (WHILE loop) but not
> closing them with a FIN/RST. However, no matter what i do, i cannot get the
> loop to stop sending FIN from the client.
>
> Any clues?
>
> Here is my current script
>
> #!/usr/bin/python
>
> import socket,sys
> from numpy import *
> num1=0
>
> while (num1<=10) :
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.settimeout(10.0)
> s.connect(("10.1.1.69", 50008)) # SMTP
> print s.recv(1024) + '\n',
> num1=num1+1
> #s.close()
>
> sys.exit(1)
socket.socket instances do an implicit close() on the socket when the
object is destructed (in this case, it's destructed when it is garbage-
collected). What's happening is that on each iteration, the variable
"s", which references the socket.socket instance, is assigned to a new
socket.socket instance, therefore the instance of the previous
iteration is no longer referenced by "s", and since it's no longer
referenced by anything, the instance is garbage-collected,
automatically imposing an implicit close() on that instance. A simple
solution could be to create a list and append the socket.socket
instance of each iteration to that list, that way the instances would
remain referenced in the list and not be garbage-collected; though you
might be able to find a more elegant solution.
Sebastian
More information about the Python-list
mailing list