新手:保持TCP套接字打开 [英] Newbie: Keep TCP socket open
问题描述
嗨伙计,
我是Python的新手,但已经成功创建了一个简单的客户端和
服务器设置,但我有一个问题。
我试图通过发送许多TCP conns(WHILE循环)来测试一个盒子,但不是用$ / b $来用一个FIN / RST来关闭它们。但是,无论我做什么,我都无法获得
循环来停止从客户端发送FIN。
任何线索?
这是我当前的脚本
#!/ usr / bin / python
import socket,sys
来自numpy进口的
*
num1 = 0
而(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)
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)
推荐答案
5月19日上午10点25分, Alan Wright < alan.wri ... @ volubill.comwrote:
On May 19, 10:25 am, "Alan Wright" <alan.wri...@volubill.comwrote:
嗨伙计,
我是Python的新手,但已成功创建一个简单的客户端和
服务器设置,我有一个问题。
我试图通过发送许多TCP conns(WHILE循环)来测试一个盒子但是没有
用FIN / RST关闭它们。但是,无论我做什么,我都无法获得
循环来停止从客户端发送FIN。
任何线索?
这是我当前的脚本
#!/ usr / bin / python
import socket,sys
来自numpy进口的
*
num1 = 0
而(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)
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实例执行隐式close()当
对象被破坏时,在套接字上(在这种情况下,当它被垃圾收集时它被破坏 -
收集)。发生的事情是,在每次迭代时,引用socket.socket实例的变量
" s"被分配给一个新的
,所以实例是垃圾收集,
自动对该实例强加隐式close()。一个简单的
解决方案可能是创建一个列表并将每个迭代的socket.socket
实例附加到该列表中,这样实例将是
仍然在列表中引用,而不是垃圾回收;虽然你可以找到更优雅的解决方案。
Sebastian
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
Alan Wright写道:
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))#SMS
print s.recv(1024)+''\ n'',
num1 = num1 + 1
#s.close()
sys.exit(1)
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)
我认为发生了以下情况:
重用每个新套接字的's'对象将使Python变为垃圾
收集以前的套接字。收集套接字的垃圾可能会关闭()它。
创建所有套接字后,程序也会退出。我想Python或者
操作系统本身都会关闭所有套接字。
尝试将你制作的每个新套接字放入一个大的列表中,这样Python就可以了'$
垃圾收集它。最后让你的程序进入睡眠状态。
导入时间
allsockets = []
while( ...):
s = socket.socket(...
allsockets.append(s)
s.settimeout(...
...
time.sleep(99999)
--irmen
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
感谢您的反馈。
在列表中使用套接字很棒
但是,作为我想象一下,我现在在
系统崩溃之前得到了大约1500个康斯坦特的限制,我也注意到,当它们达到5000时,端口循环回到1025
关于如何使列表/插座达到50K左右的任何想法
TIA
艾伦
< s0 **** @ gmail.com写信息
新闻:e8 **************** ****************** @ c58g2000 hsc.googlegroups.com ...
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...
5月19日,10日:上午25点,Alan Wright< alan.wri ... @ volubill.comwro te:
On May 19, 10:25 am, "Alan Wright" <alan.wri...@volubill.comwrote:
> Hi Folks,
我是Python的新手,但是已经成功创建了一个简单的客户端和服务器设置,我有但是有一个问题。
我试图通过发送许多TCP conns(WHILE循环)来测试一个盒子,但不是用FIN / RST关闭它们。然而,无论我做什么,我都无法停止从客户端发送FIN。
任何线索?
这是我当前的脚本
#/ / usr / bin / python
导入socket,sys
来自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)
>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实例在套接字上执行隐式close()
对象被破坏(在这种情况下,当它是垃圾时它被破坏 -
收集)。发生的事情是,在每次迭代时,引用socket.socket实例的变量
" s"被分配给一个新的
,所以实例是垃圾收集,
自动对该实例强加隐式close()。一个简单的
解决方案可能是创建一个列表并将每个迭代的socket.socket
实例附加到该列表中,这样实例将是
仍然在列表中引用,而不是垃圾回收;虽然你可以找到更优雅的解决方案。
Sebastian
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
这篇关于新手:保持TCP套接字打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!