所以我想在发送多个数据包又一遍我的虚拟机,但一次尝试之后,我得到的错误:再发送数据
Traceback (most recent call last):
File "SMB_Test2.py", line 157, in <module>
s.sendall(SMB_COM_NEGOTIATE)
File "C:\Python27\Lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host
,我推测是由于重复畸形数据被发送(有意),但我想知道是否以及如何解决这个问题。我基本上希望多次发送SMB_COM_NEGOTIATE。提前致谢。
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((addr, port))
s.settimeout(2)
print '[*] Connected to "%s:%d".' % (addr, port)
s.sendall(SMB_COM_NEGOTIATE)
a = 0
while a != 50000:
print a
a = a + 1
s.sendall(SMB_COM_NEGOTIATE)
print '[*] Sent to "%s:%d".' % (addr, port)
EDIT(关闭詹姆斯的建议) - 静止跳转权利一个错误:
a = 0
try:
print "The value of 'a' is %r." % a
s.connect((addr, port))
print '[*] Connected to "%s:%d".' % (addr, port)
while a != 50000:
a = a + 1
s.sendall(SMB_COM_NEGOTIATE)
print '[*] Sent to "%s:%d".' % (addr, port)
print "The value 'a' is %r." % a
except:
print "[-] An error occured!!!"
s.close()
exit()
输出:
The value of 'a' is 0.
[*] Connected to "192.168.xxx.xxx:xxx".
[*] Sent to "192.168.xxx.xxx:xxx".
The value 'a' is 1.
[-] An error occured!!!
还试图此(几乎相同):
a = 0
print "The value of 'a' is %r." % a
s.connect((addr, port))
print '[*] Connected to "%s:%d".' % (addr, port)
def ok():
try:
while a != 50000:
a = a + 1
s.sendall(SMB_COM_NEGOTIATE)
print '[*] Sent to "%s:%d".' % (addr, port)
print "The value 'a' is %r." % a
except:
print "[-] An error occured!!!"
sleep(0)
s.close()
其中有一个输出(甚至不发送什么东西):
The value of 'a' is 0.
[*] Connected to "192.168.xxx.xxx:xxx".
[-] An error occurred!!!
捕获异常,关闭套接字,获取一个新套接字并重新连接。在python中查找数以百万计的文章。 [Here](http://www.pythonforbeginners.com/error-handling/python-try-and-except)就是其中之一。 –
@JamesKPolk更新 – Russell
好吧,显然“获得一个新的套接字并再次连接”需要在循环内。 –