I'm trying to remote into an interactive shell and import modules within python 2.7. I'm getting hung up. So far this is what I've got:
import rpyc
import socket
hostname = socket.gethostname()
port = 12345
connections = rpyc.connect(hostname,port)
session = connections.root.getSession()
session exists
>>>session
<blah object at 0xMore-Goop>
I want to issue an import sys so I can add another module to the path. However when I try to see if modules exist in the path I get the following:
>>>connections.modules
AttributeError: 'Connection' object has no attribute 'modules'
What I need to execute remotely is the following:
import sys
sys.path.append(path/to/import)
import file
log = file.logger(session, path/to/log)
Is it possible to have rpyc issue the above content? Thanks in advance
You can add the following methods to the service:
import sys, importlib, rpyc
...
class MyService(rpyc.Service):
...
def exposed_import_module(self, mod):
return importlib.import_module(mod)
def exposed_add_to_syspath(self, path):
return sys.path.append(path)
and access it like this:
connections.root.add_to_syspath('path/to/import')
file = connections.root.import_module('file')
file.logger(session, 'path/to/log')
Related
I have created 2 classes
Connections.py and LogObserver.py
I am trying to import Connections.py in LogObserver.py but python keep throwing an error
ModuleNotFoundError: No module named 'connections'
The way i am importing it is
from connections.Connections import Connections
class LogObserver:
The file structure is
If your Class name in Connections file is called Connections then you can try following:
from connections import Connections
c = Connections.Connections()
Or:
import connections.Connections as myModule
c = myModule.Connections()
make sure when you import that you do following:
from <folder>.<filename> import <class_name>
There is a problem in your file structure.
Try keeping connections folder inside queries folder OR specify the file path for connections.py correctly.
Hope it resolves the issue.
There is a python module which follows as x.y.z in which a function is present :-
from ftplib import FTP
def connect(host,user,pass) :
ftp = FTP(host, user, pass)
Test case is follows :-
classs testClass(unittest.TestCase)
#patch(x.y.z.FTP)
def test_connect(self,MOCKFTP) :
mock_ftp_obj = MOCKFTP()
connect('fakehost','fakeuser','fakepass')
Running the test case gives - 530 login incorrect !
Am I missing something.
The issue is incorrect path value x.y.z.FTP FTP is actually present in ftplib package.
Following works as expected:
x.y.z.py:
import ftplib
def connect(host, user, pass1):
ftp = ftplib.FTP(host=host, user=user, passwd=pass1)
testz.py:
import unittest
from unittest.mock import patch
from x.y.z import connect
class testClass(unittest.TestCase):
#patch('ftplib.FTP', autospec=True)
def test_download_file(self, mock_ftp):
connect('fakehost','fakeuser','fakepass')
mock_ftp.assert_called_with('fakehost','fakeuser','fakepass')
Please let me know if it helps.
I'm trying to connect to biometric device. I have installed 'Zklib' using (Pip). My code as follows
`import sys
import zklib
import time
from zklib import zkconst
zk = zklib.ZKLib("192.168.0.188", 4370)
ret = zk.connect()
print "connection:", ret`
When I execute this, I get an error
AttributeError: 'module' object has no attribute 'ZKLib'
Help me to run this code successfully.
Try the following instead:
import sys
import time
from zklib import zklib, zkconst
zk = zklib.ZKLib("192.168.0.188", 4370)
ret = zk.connect()
print "connection:", ret
The ZKlib class is in zklib.zklib not zklib. It appears that there is a typo in the Getting Started section of their GitHub page (or they expect you to be in the zklib directory when running your code?).
I've been playing around with IPython.parallel and I wanted to use some custom modules of my own, but haven't been able to do it as explained on the cookbook using dview.sync_imports(). The only thing that has worked for me was something like
def my_parallel_func(args):
import sys
sys.path.append('/path/to/my/module')
import my_module
#and all the rest
and then in the main just to
if __name__=='__main__':
#set up dview...
dview.map( my_parallel_func, my_args )
The correct way to do this would in my opinion be something like
with dview.sync_imports():
import sys
sys.path.append('/path/to/my/module')
import my_module
but this throws an error saying there is no module named my_module.
So, what is the right way of doing it using dview.sync_imports()??
The problem is that you're changing the PYTHONPATH just in the local process running the Client, and not in the remote processes running in the ipcluster.
You can observe this behaviour if you run the next piece of code:
from IPython.parallel import Client
rc = Client()
dview = rc[:]
with dview.sync_imports():
import sys
sys.path[:] = ['something']
def parallel(x):
import sys
return sys.path
print 'Local: ', sys.path
print 'Remote: ', dview.map_sync(parallel, range(1))
Basically all the modules that you want to use with sync_imports must already be in the PYTHONPATH.
If it's not in the PYTHONPATH then you must add it to the path in the function that you execute remotely, and then import the module in the function.
Grinder is new for me and I am trying to figure out how to get rid of this error:
my test.py script:
import string
import random
from java.lang import String
from java.net import URLEncoder
from net.grinder.plugin.http import HTTPRequest
from net.grinder.common import GrinderException
log = grinder.logger.info
stat = grinder.statistics.forLastTest
SERVER = "http://www.google.com"
URI = "/"
class TestRunner:
def __call__(self):
requestString = "%s%s" % (SERVER, URI)
request = HTTPRequest()
result = request.GET(requestString)
if string.find(result.getText(), "SUCCESS") < 1:
stat.setSuccess(0)
I run
java net.grinder.Console
java net.grinder.Grinder
in my localhost.
after starting the test, this message keeps popping up:
aborting process - Jython exception, <type 'exceptions.NameError'>: name 'grinder' is not defined [initialising test script]
net.grinder.scriptengine.jython.JythonScriptExecutionException: <type 'exceptions.NameError'>: name 'grinder' is not defined
log = grinder.logger.info
File "./test.py", line 8, in <module>
It looks like I have to include some Grinder module for this "grinder.logger.info" but I just have no clue of what I should import... ...
Any hint?
Thanks in advance
you imported items from grinder, not grinder itself, try
import grinder.logger.info
import grinder.statistics.forLastTest
it might also be net.grinder.logger.info and net.grinder.statistics.forLastTest if this is the case then your code will need changing to go with the change from grinder to net.grinder
You have not imported grinder.
from net.grinder.script.Grinder import grinder
and now try again.