我试图在 SecureCRT 的登录脚本中使用套接字模块。从我的命令行运行时,脚本运行完美,但是一旦我尝试通过 SecureCRT 运行它,它就会告诉我没有套接字模块。我使用 python 3.7 并确保套接字在我的库中。我的库也映射为路径。
这是我正在运行的脚本:
import socket
import datetime
timeIn=(datetime.datetime.now().strftime('%d %B %Y %H:%M:%S'))
hostname = socket.gethostname()
hostip = socket.gethostbyname(hostname)
t=open("secCRT.txt", "w")
t.write('testing script \n')
t.write(timeIn)
t.write(' host: '+ hostname)
t.write(' ip: '+ hostip)
t.close()
从我的命令行运行时,它运行良好,但在 crt 中运行时,它表明没有名为 socket 的模块(没有名为 _socket 的模块)。我看到有与此类似的帖子,但没有一个能够帮助我。
编辑#1
这是python映射到路径系统变量的方式:
编辑#2
尝试将 3.7 版的 _socket.py 和 socket.py 移动到与脚本相同的目录,但仍然出现错误。编辑*也是socket.cpython-37.pyc
编辑#3
我想知道,因为这是一个登录脚本(在与服务器建立连接时运行),它是否正在寻找我连接的服务器上的套接字模块而不是本地机器?脚本本身在本地机器上。
编辑#4:从命令行而不是登录脚本
>>> import sys
>>> import pprint
>>> pprint.pprint(sys.path)
['',
'C:\\Program Files\\Python\\Python37\\python37.zip',
'C:\\Program Files\\Python\\Python37\\DLLs',
'C:\\Program Files\\Python\\Python37\\lib',
'C:\\Program Files\\Python\\Python37',
'C:\\Program Files\\Python\\Python37\\lib\\site-packages']
编辑#5
我能够在 SecureCRT 应用程序中作为脚本执行此操作
with open("secCRT.txt", "w") as sout:
sout.write(pprint.pformat(vars(pprint)))
有几行引用了这个文件:
C:\Program Files\VanDyke Software\Clients\vpython27.zip
这让我相信他们使用的是 2.7 版。当我在里面vpython27.zip
搜索“socket”时socket.pyc
,SocketServer.pyc
这是唯一出现的项目。这是否意味着我想socket.py
为 2.7 版找到一个或任何其他依赖项并将它们移到那里?
编辑#6:开发人员(VanDyke)的解释
" - The _socket module is built out by default as a .pyd
file on Windows. This is effectively a .dll that can be
loaded by the Python interpreter. Unfortunately, .pyd's
can *not* be loaded out of the Python distribution zip
file we ship."
编辑#7:这是 VanDyke 建议我获取我正在寻找的数据的方式
objTab=crt.GetScriptTab()
objConfig=objTab.Session.Config
strHostname=objConfig.GetOption('Hostname')
strSessName=objTab.Session.Path
这个解决方案非常适合我的环境。