Python 在套接字中重新发送数据

Python 在套接字中重新发送数据,python,python-2.7,sockets,virtual-machine,smb,Python,Python 2.7,Sockets,Virtual Machine,Smb,因此,我尝试一次又一次地向我的VM发送多个数据包,但在一次尝试后,我得到了错误: 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)(

因此,我尝试一次又一次地向我的VM发送多个数据包,但在一次尝试后,我得到了错误:

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
编辑(根据Jame的建议)-仍然会跳转到一个错误:

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!!!
还尝试了以下方法(几乎相同):

有输出(甚至不发送任何内容):


下面是一段代码片段来说明我的评论

import socket

def try_connect():
    """Tries to connect and send the SMB_COM_NEGOTIATE bytes.
       Returns the socket object on success and None on failure.
    """
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)
    try:
        s.connect((addr, port))
        s.sendall(SMB_COM_NEGOTIATE)
    except socket.timeout as e:
        # got a socket timeout
        return None
    except OSError as e:
        # got some other socket error
        return None
    return s

def try_connect_n_times(n):
    """Try up to n times to connect"""
    for attempt in range(n):
        s = try_connect()
        if s:
            return s
    return None

try_connect_n_times(5000)

下面是一段代码片段来说明我的评论

import socket

def try_connect():
    """Tries to connect and send the SMB_COM_NEGOTIATE bytes.
       Returns the socket object on success and None on failure.
    """
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)
    try:
        s.connect((addr, port))
        s.sendall(SMB_COM_NEGOTIATE)
    except socket.timeout as e:
        # got a socket timeout
        return None
    except OSError as e:
        # got some other socket error
        return None
    return s

def try_connect_n_times(n):
    """Try up to n times to connect"""
    for attempt in range(n):
        s = try_connect()
        if s:
            return s
    return None

try_connect_n_times(5000)

捕获异常,关闭套接字,获取新套接字并再次连接。查找关于try/的数百万篇文章中的任何一篇,python除外。是一个。@JamesKPolk updated很明显,“获取新套接字并再次连接”需要在循环中。@JamesKPolk当你说“获取新套接字并再次连接”时,你指的是一个全新的addr+端口还是关闭套接字并再次连接回它?捕获异常,关闭套接字,换一个新插座,然后重新连接。查找关于try/的数百万篇文章中的任何一篇,python除外。是一个。@JamesKPolk updated很明显,“获取新套接字并再次连接”需要在循环中。@JamesKPolk当你说“获取新套接字并再次连接”时,你指的是一个全新的addr+端口还是关闭套接字并再次连接到它?谢谢你的帮助帖子,在我删除s=和if s:返回s之后,它完全按照我希望的方式工作。感谢您的时间和帮助,非常感谢:)。如果您感兴趣,请附带问题:我希望每次调用时都发送一个新的s.sendall(SMB\u COM\u协商)。我试着用def调用它,但没有成功(正如wireshark中分析的那样)。想法?谢谢你的帮助帖子,在我删除s=和if s:返回s后,它完全按照我希望的那样工作。感谢您的时间和帮助,非常感谢:)。如果您感兴趣,请附带问题:我希望每次调用时都发送一个新的s.sendall(SMB\u COM\u协商)。我试着用def调用它,但没有成功(正如wireshark中分析的那样)。思想?
The value of 'a' is 0.
[*] Connected to "192.168.xxx.xxx:xxx".
[-] An error occurred!!!
import socket

def try_connect():
    """Tries to connect and send the SMB_COM_NEGOTIATE bytes.
       Returns the socket object on success and None on failure.
    """
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)
    try:
        s.connect((addr, port))
        s.sendall(SMB_COM_NEGOTIATE)
    except socket.timeout as e:
        # got a socket timeout
        return None
    except OSError as e:
        # got some other socket error
        return None
    return s

def try_connect_n_times(n):
    """Try up to n times to connect"""
    for attempt in range(n):
        s = try_connect()
        if s:
            return s
    return None

try_connect_n_times(5000)