python - 将 RDPY 移植到 Python3 - AttribueError
问题描述
我决定自己接手一个项目来解决一些原作者似乎已经放弃的问题。努力是将代码移植到 Python3 并相应地更新库。
下面是一段我似乎无法弄清楚的代码块。
我已经尝试过 StringsIO 和 BytesIO ,但似乎都不起作用。
class Stream(BytesIO):
"""
@summary: Stream use to read all types
"""
def dataLen(self):
"""
@return: not yet read length
"""
return self.len - self.pos
def readLen(self):
"""
@summary: compute already read size
@return: read size of stream
"""
return self.pos
def readType(self, value):
"""
@summary: call specific read on type object
or iterate over tuple elements
rollback read if error occurred during read value
@param value: (tuple | Type) object
"""
# read each tuple
if isinstance(value, tuple) or isinstance(value, list):
for element in value:
try:
self.readType(element)
except Exception as e:
# rollback already readed elements
for tmpElement in value:
if tmpElement == element:
break
self.pos -= sizeof(tmpElement)
raise e
return
# optional value not present
if self.dataLen() == 0 and value._optional:
return
value.read(self)
def readNextType(self, t):
"""
@summary: read next type but didn't consume it
@param t: Type element
"""
self.readType(t)
self.pos -= sizeof(t)
def writeType(self, value):
"""
@summary: Call specific write on type object
or iterate over tuple element
@param value: (tuple | Type)
"""
# write each element of tuple
if isinstance(value, tuple) or isinstance(value, list):
for element in value:
self.writeType(element)
return
value.write(self)
错误:
[*] DEBUG: Build size map
Traceback (most recent call last):
File "/usr/local/bin/rdpy-rdphoneypot.py", line 197, in <module>
size = readSize(arg)
File "/usr/local/bin/rdpy-rdphoneypot.py", line 154, in readSize
e = r.nextEvent()
File "/usr/local/lib/python3.7/dist-packages/rdpy/core/rss.py", line 303, in nextEvent
if self._s.dataLen() == 0:
File "/usr/local/lib/python3.7/dist-packages/rdpy/core/type.py", line 914, in dataLen
return self.len - self.pos
AttributeError: 'Stream' object has no attribute 'len'
所有代码都在这里:https ://github.com/hackdefendr/rdpy.git
任何关于为什么这不起作用的指针?
解决方案
错误信息很清楚。没有self.len
在一个BytesIO
或StringIO
对象上这样的东西。Python 2 的StringIO
对象上有,但文档中没有。也没有self.pos
, 这对您来说将是一个更大的问题,因为该代码假定您不仅可以从中读取,还可以对其进行写入。
seek()
您可以从和函数中获得许多相同的tell()
功能。tell()
相当于获取 的值pos
,seek()
相当于设置 的值pos
,再加上可以seek()
用来获取长度:
from io import StringIO
s = StringIO('test')
s.seek(2) # seek to absolute position (pos = 2)
s.seek(4, 1) # seek to relative position (pos += 4)
current_pos = s.tell()
length = s.seek(0, 2) # seeks to end and returns the position
s.seek(current_pos) # returns to previous position
推荐阅读
- search - 自定义 docusaurus 搜索栏 v2
- flutter - 如何使用 Flutter Riverpod 充当 ChangeNotifierProxy Provider?
- html - 如何在圆圈上方添加文字?
- flutter - 使用不包含媒体查询的上下文调用 mediaquery.of()。从上下文开始找不到媒体查询
- amazon-web-services - 为什么我收到 aws ExpiredToken:请求中包含的安全令牌已过期?
- sql - 列的 T-SQL 属性天
- r - 将系统控制台输出返回到用户界面
- amazon-web-services - 如何让 AWS API 网关“理解”斜杠?
- r-markdown - 如何删除 tint::tintHtml 在 kable 表中的单元格周围添加的白色填充/边框?
- web - 如何减少 PWA 网站空间使用量?PWA 占用巨大空间