【问题标题】:Resending data in socket在套接字中重新发送数据
【发布时间】:2017-07-20 18:59:38
【问题描述】:

所以我试图一遍又一遍地向我的虚拟机发送多个数据包,但在一次尝试之后,我得到了错误:

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)

编辑(根据 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!!!

也试过这个(几乎相同):

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!!!

【问题讨论】:

  • 捕获异常,关闭socket,获取新socket,重新连接。在 python 中查找有关 try/except 的数百万篇文章中的任何一篇。 Here 是一个。
  • @JamesKPolk 更新
  • 好吧,显然“获取新套接字并再次连接”需要在循环内。
  • @JamesKPolk 当你说“获取一个新的套接字并再次连接”时,你是指一个全新的 addr + 端口还是关闭套接字并再次连接到它?

标签: python python-2.7 sockets virtual-machine smb


【解决方案1】:

这是一个代码片段来说明我的评论。

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)

【讨论】:

  • 感谢您提供有用的帖子,在删除 for s = 和 if s: return s 后完全按照我的意愿工作。感谢您的时间和帮助,非常感谢:)。
  • 如果您有兴趣,请补充问题:我希望每次调用时都发送一个新的 s.sendall(SMB_COM_NEGOTIATE)。我试过用 def 调用它,但是没有用(正如在 wireshark 中分析的那样)。想法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多