473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Newbie: Keep TCP socket open

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)
Jun 27 '08 #1
13 8960
On May 19, 10:25 am, "Alan Wright" <alan.wri...@volubill.comwrote:
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
Jun 27 '08 #2

Alan Wright wrote:
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)
I think the following is happening:
Reusing the 's' object for every new socket will make Python to garbage
collect the previous ones. Garbage collecting a socket will likely close() it.
Also after creating all sockets your program exits. I guess either Python or the
operating system itself will go close all the sockets.
Try putting every new socket you make into a big list instead, so that Python can't
garbage collect it. And put your program to sleep at the end.

import time
allsockets=[]

while (...):
s=socket.socket(...
allsockets.append(s)
s.settimeout(...
...

time.sleep(99999)

--irmen
Jun 27 '08 #3
Thanks for the feedback.

Using the socket in a list is great

However, as i imagined, I now get a limit of around 1500 conns before the
system crashes out, also i have noticed, that the ports loop back to 1025
when they hit 5000.

Any ideas on how to make the list/socket get to around 50K

TIA

Alan
<s0****@gmail.comwrote in message
news:e8**********************************@c58g2000 hsc.googlegroups.com...
On May 19, 10:25 am, "Alan Wright" <alan.wri...@volubill.comwrote:
>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

Jun 27 '08 #4
On Mon, 19 May 2008 20:25:57 +0100
"Alan Wright" <al*********@volubill.comwrote:
Thanks for the feedback.

Using the socket in a list is great

However, as i imagined, I now get a limit of around 1500 conns before
the system crashes out, also i have noticed, that the ports loop back
to 1025 when they hit 5000.

Any ideas on how to make the list/socket get to around 50K

TIA
Try to use scapy to send raw empty packets with S flag set.
Also use Linux/BSD if you're trying this on Windows.

--
Regards,
Ghirai.
Jun 27 '08 #5
Ghirai,
Scapy does the same, only it sends RST and not FIN, so still no help

send(IP(dst="10.1.1.2")/TCP(dport=50000,flags="S"))

Only have windows at the moment sadly.

Alan

"Ghirai" <gh****@ghirai.comwrote in message
news:ma***************************************@pyt hon.org...
On Mon, 19 May 2008 20:25:57 +0100
"Alan Wright" <al*********@volubill.comwrote:
>Thanks for the feedback.

Using the socket in a list is great

However, as i imagined, I now get a limit of around 1500 conns before
the system crashes out, also i have noticed, that the ports loop back
to 1025 when they hit 5000.

Any ideas on how to make the list/socket get to around 50K

TIA

Try to use scapy to send raw empty packets with S flag set.
Also use Linux/BSD if you're trying this on Windows.

--
Regards,
Ghirai.

Jun 27 '08 #6
On Mon, 19 May 2008 23:50:50 +0100
"Alan Wright" <al*********@volubill.comwrote:
Ghirai,
Scapy does the same, only it sends RST and not FIN, so still no help

send(IP(dst="10.1.1.2")/TCP(dport=50000,flags="S"))

Only have windows at the moment sadly.

Alan
Are you sure there's no firewall or something else between you and the
remote host?

Because i just tried that command with scapy and it didn't send any other packets
except what it was told (1 packet with SYN flag set).

I haven't tried on windows though.

--
Regards,
Ghirai.
Jun 27 '08 #7
In article <DK******************************@pipex.net>,
"Alan Wright" <al*********@volubill.comwrote:
Thanks for the feedback.

Using the socket in a list is great

However, as i imagined, I now get a limit of around 1500 conns before the
system crashes out, also i have noticed, that the ports loop back to 1025
when they hit 5000.

Any ideas on how to make the list/socket get to around 50K
Yikes. Not on any box I know of. A given process is limited in how many
descriptors it can have open at once. I don't know of any that will allow
anywhere near 50k. Somewhere in the 1-2000 range would be more typical.
The 1500 you report is not at all surprising.

You might try creating a bunch of child processes with os.system() or
something of that ilk. Create 50 processes and have each one open 1000
sockets.

The next thing you have to worry about is whether the OS can handle 50k
file descriptors open per-system. Or 50k sockets, or TCP connections. I
wouldn't be too surprised if many systems couldn't. The address space (TCP
port numbers) is 16-bit (unsigned), or about 65k, but you may well run into
some other system limit long before you exhaust the theoretically available
ports.

Something like Scapy, recommended by others, may indeed be able to generate
all those SYN packets you want, but that doesn't mean you'll get all the
open connections you seek. You send a SYN packet to the remote host, and
it sends back a SYN/ACK. The local kernel now sees a SYN/ACK packet for a
port it doesn't know about. I'm not sure what the RFCs say about that, but
I wouldn't be surprised if the kernel ends up sending a RST or maybe a FIN
or something like that. The kernel owns the ports; it's not nice to try
and mess with them on your own.
Jun 27 '08 #8
Thanks Roy

Any ideas how to code this child process stuff, as I said I am newbie and
not from a coding background

to be honest ideally yes, i'd get 50K, but if i can get above 30K that would
be OK

Alan

"Roy Smith" <ro*@panix.comwrote in message
news:ro***********************@70-1-84-166.area1.spcsdns.net...
In article <DK******************************@pipex.net>,
"Alan Wright" <al*********@volubill.comwrote:
>Thanks for the feedback.

Using the socket in a list is great

However, as i imagined, I now get a limit of around 1500 conns before the
system crashes out, also i have noticed, that the ports loop back to 1025
when they hit 5000.

Any ideas on how to make the list/socket get to around 50K

Yikes. Not on any box I know of. A given process is limited in how many
descriptors it can have open at once. I don't know of any that will allow
anywhere near 50k. Somewhere in the 1-2000 range would be more typical.
The 1500 you report is not at all surprising.

You might try creating a bunch of child processes with os.system() or
something of that ilk. Create 50 processes and have each one open 1000
sockets.

The next thing you have to worry about is whether the OS can handle 50k
file descriptors open per-system. Or 50k sockets, or TCP connections. I
wouldn't be too surprised if many systems couldn't. The address space
(TCP
port numbers) is 16-bit (unsigned), or about 65k, but you may well run
into
some other system limit long before you exhaust the theoretically
available
ports.

Something like Scapy, recommended by others, may indeed be able to
generate
all those SYN packets you want, but that doesn't mean you'll get all the
open connections you seek. You send a SYN packet to the remote host, and
it sends back a SYN/ACK. The local kernel now sees a SYN/ACK packet for a
port it doesn't know about. I'm not sure what the RFCs say about that,
but
I wouldn't be surprised if the kernel ends up sending a RST or maybe a FIN
or something like that. The kernel owns the ports; it's not nice to try
and mess with them on your own.

Jun 27 '08 #9
Same on FC8, sends RST after it sees SYN/ACK

"Ghirai" <gh****@ghirai.comwrote in message
news:ma***************************************@pyt hon.org...
On Mon, 19 May 2008 23:50:50 +0100
"Alan Wright" <al*********@volubill.comwrote:
>Ghirai,
Scapy does the same, only it sends RST and not FIN, so still no help

send(IP(dst="10.1.1.2")/TCP(dport=50000,flags="S"))

Only have windows at the moment sadly.

Alan

Are you sure there's no firewall or something else between you and the
remote host?

Because i just tried that command with scapy and it didn't send any other
packets
except what it was told (1 packet with SYN flag set).

I haven't tried on windows though.

--
Regards,
Ghirai.

Jun 27 '08 #10
In article <ia******************************@pipex.net>,
"Alan Wright" <al*********@volubill.comwrote:
Thanks Roy

Any ideas how to code this child process stuff, as I said I am newbie and
not from a coding background
The easiest thing would be to use os.system(). If you wanted to spawn 10
child processes, you could do:

import os
for i in range(10):
os.system ("./child.py &")

and then have child.py be a script that creates 1000 TCP connections.

Keep in mind that one man's stress test is another man's denial of service
attack. If there are any firewalls between you and your target, they may
restrict the number of connections you get to make (or the rate at which
they're created). You may also get a polite phone call from your local IT
people asking enquiring about your activities.
Jun 27 '08 #11
You must have something in your IPtables

I needed to put a rule in to drop these unwanted RST from getting back out.

All fixed now

Thanks for the advice

Alan

"Alan Wright" <al*********@volubill.comwrote in message
news:ia******************************@pipex.net...
Same on FC8, sends RST after it sees SYN/ACK

"Ghirai" <gh****@ghirai.comwrote in message
news:ma***************************************@pyt hon.org...
>On Mon, 19 May 2008 23:50:50 +0100
"Alan Wright" <al*********@volubill.comwrote:
>>Ghirai,
Scapy does the same, only it sends RST and not FIN, so still no help

send(IP(dst="10.1.1.2")/TCP(dport=50000,flags="S"))

Only have windows at the moment sadly.

Alan

Are you sure there's no firewall or something else between you and the
remote host?

Because i just tried that command with scapy and it didn't send any other
packets
except what it was told (1 packet with SYN flag set).

I haven't tried on windows though.

--
Regards,
Ghirai.


Jun 27 '08 #12
Thanks Roy, will give it a go.

infact there is no need for any IT phone calls, I am the owner of this
network

Very simple [bunch of clients]----[box under test]----[bunch of servers]

Now i should be able to hammer them ;)

Alan

"Roy Smith" <ro*@panix.comwrote in message
news:ro***********************@70-1-84-166.area1.spcsdns.net...
In article <ia******************************@pipex.net>,
"Alan Wright" <al*********@volubill.comwrote:
>Thanks Roy

Any ideas how to code this child process stuff, as I said I am newbie and
not from a coding background

The easiest thing would be to use os.system(). If you wanted to spawn 10
child processes, you could do:

import os
for i in range(10):
os.system ("./child.py &")

and then have child.py be a script that creates 1000 TCP connections.

Keep in mind that one man's stress test is another man's denial of service
attack. If there are any firewalls between you and your target, they may
restrict the number of connections you get to make (or the rate at which
they're created). You may also get a polite phone call from your local IT
people asking enquiring about your activities.

Jun 27 '08 #13
In article <3K*********************@pipex.net>,
"Alan Wright" <al*********@volubill.comwrote:
infact there is no need for any IT phone calls, I am the owner of this
network
That's the best way to do it :-)
Jun 27 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jane Austine | last post by:
Running Python 2.3 on Win XP It seems like socket is working interdependently with subprocesses of the process which created socket. ------------------------------------ #the server side >>>...
3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
2
by: Jean-Philippe Guyon | last post by:
Hello, I am trying to compile a class that uses socket using the Visual C++ ..NET compiler. I get the following error: ------ Build started: Project: infCommon, Configuration: Release Win32...
4
by: zelzel.zsu | last post by:
I wrote two simple socket program. one for sending a file and the other for receiving the file. but when I run it, a curious thing happened. The received file was samller that the sent file. $...
9
by: AA | last post by:
This is making me crazy!! Please, if some body can help me. I'm testing a ver simple socket client. In my test I just open and close a connection (in a loop) to my local IIS server (port 80)...
1
by: Techsol | last post by:
Hi, I have synchronous communications between a server and client. To save bandwith the connection must persist. So the socket must stay open and only be re-opened in case of communications failure....
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my...
0
by: Jaap Spies | last post by:
Hi, Running Fedora Core 4: Python 2.4.3 and Python 2.4.1. I'm getting: IOError: (2, 'No such file or directory') all the time. Trying to track down this problem: Python 2.4.1 (#1, May 16...
4
by: O.B. | last post by:
I have a socket configured as TCP and running as a listener. When I close socket, it doesn't always free up the port immediately. Even when no connections have been made to it. So when I open...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.