Search code examples
python-3.xpython-2.7secure-crt

No Module Named Socket


Im trying to use the socket module in a logon script for SecureCRT. When run from my command line, the script runs perfectly but once I try to run it through SecureCRT it tells me there is no socket module. Im using python 3.7 and have made sure socket is in my lib. My lib is also mapped for path.

Here is the script Im running:

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()

When run from my command line it works perfectly but when run inside crt, it states there is no module named socket (no module named _socket). Ive seen there are similar posts to this but none that have been able to help me yet.

EDIT #1

Here is the way python is mapped to the path system variable:

enter image description here

EDIT #2

Tried moving _socket.py and socket.py for version 3.7 to the same directory as the script but still getting errors. edit* also socket.cpython-37.pyc

EDIT #3

Im wondering, because this is a logon script (run when a connection is made to a server), could it be looking for the socket module on the server im connected to instead of the local machine? The script itself is on the local machine.

EDIT #4 : from command line not logon script

>>> 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']

EDIT #5

I was able to do this as a script within the SecureCRT application

with open("secCRT.txt", "w") as sout:
    sout.write(pprint.pformat(vars(pprint)))

Several of the lines reference this file:

C:\Program Files\VanDyke Software\Clients\vpython27.zip

This leads me to believe they are using verison 2.7. When I searched inside vpython27.zip for "socket" the socket.pyc and SocketServer.pyc were the only items that came up. Does this mean I would want to find a socket.py or any other dependencies for version 2.7 and move them there?

EDIT #6 : explanation from developer (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."

EDIT #7 : here is how VanDyke suggested I get the data I was looking for

objTab=crt.GetScriptTab()
objConfig=objTab.Session.Config
strHostname=objConfig.GetOption('Hostname')
strSessName=objTab.Session.Path

This solution works great for my environment.


Solution

  • WORKING SOLUTION

    VanDyke suggested I use the following code to get the Hostname and Session name with python used as a logon script.

    objTab=crt.GetScriptTab()
    objConfig=objTab.Session.Config
    strHostname=objConfig.GetOption('Hostname')
    strSessName=objTab.Session.Path