I have a twisted tac file (twisted_service.py) with a code:
from twisted.application import service
# application.py file in the same dir
from .application import setup_reactor
class WebsocketService(service.Service):
def startService(self):
service.Service.startService(self)
setup_reactor()
application = service.Application("ws")
ws_service = WebsocketService()
ws_service.setServiceParent(application)
Here is application.py file, which sets up the reactor:
# -*- coding: utf-8 -*-
from twisted.web.server import Site
from twisted.web.static import Data
from twisted.internet import reactor, defer
from autobahn.twisted.resource import WebSocketResource
from autobahn.twisted.websocket import WebSocketServerFactory
from txsni.snimap import SNIMap
from txsni.maputils import Cache
from txsni.snimap import HostDirectoryMap
from twisted.python.filepath import FilePath
from tools.database.async import pg_conn
from tools.database import makedsn
from tools.config import main_db
from tools.modules.external import flask_setup
import tools.config as config
import websockethandlers as wsh
from pytrapd import TrapsListener
PROTOCOLMAP = {
'portcounters': wsh.PortCounters,
'eqcounters': wsh.EquipmentCounters,
'settings': wsh.Settings,
'refresh': wsh.Refresher,
'montraps': wsh.TrapsMonitoring,
'fdbs': wsh.FdbParser,
'portstate': wsh.PortState,
'cable': wsh.CableDiagnostic,
'eqcable': wsh.EquipmentCableDiagnostic,
'igmp': wsh.Igmp,
'ipmac': wsh.IpMac,
'lldp': wsh.LLDPParser,
'alias': wsh.AliasSetup,
'ping': wsh.Ping
}
#defer.inlineCallbacks
def setup_reactor():
flask_setup()
yield pg_conn.connect(makedsn(main_db))
root = Data("", "text/plain")
for key in PROTOCOLMAP:
factory = WebSocketServerFactory("wss://localhost:%s" % config.ws_port)
factory.protocol = PROTOCOLMAP[key]
resource = WebSocketResource(factory)
root.putChild(key, resource)
site = Site(root)
context_factory = SNIMap(
Cache(HostDirectoryMap(FilePath(config.certificates_directory)))
)
reactor.listenSSL(config.ws_port, site, context_factory)
traps_listener = TrapsListener()
traps_listener.listen_traps(config.trap_ip)
traps_listener.listen_messages(config.fifo_file)
if __name__ == '__main__':
setup_reactor()
import sys
from twisted.python import log
log.startLogging(sys.stdout)
reactor.run()
I use twistd -noy twisted_service.py command to run the twisted service. It has been working for twisted 16.3.2 version. After upgrade to any next version I got the error:
Unhandled Error
Traceback (most recent call last):
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/app.py", line 662, in run
runApp(config)
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/scripts/twistd.py", line 25, in runApp
_SomeApplicationRunner(config).run()
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/app.py", line 380, in run
self.application = self.createOrGetApplication()
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/app.py", line 445, in createOrGetApplication
application = getApplication(self.config, passphrase)
--- <exception caught here> ---
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/app.py", line 456, in getApplication
application = service.loadApplication(filename, style, passphrase)
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/service.py", line 412, in loadApplication
application = sob.loadValueFromFile(filename, 'application')
File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/persisted/sob.py", line 177, in loadValueFromFile
eval(codeObj, d, d)
File "twisted_service.py", line 3, in <module>
from .application import setup_reactor
exceptions.ImportError: No module named application
How should I run the twisted or import module properly?
What the answer I found nearly yours. like the follows:
import os
import sys
sys.path = [os.path.join(os.getcwd(), '.'), ] + sys.path
just add the current working directory to the sys.path.
But I have not found more better method.... I think this not very good.
I found the answer here http://twistedmatrix.com/pipermail/twisted-python/2016-September/030783.html
It is a new feature in Twisted 16.4.0. In previous versions twistd script automatically added working dir to system paths, from 16.4.0 version i have to added it manually. It can be added something like this in twisted_service.py file:
import os
import sys
TWISTED_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(TWISTED_DIR)
Related
I have the following project structure:
within blog_posts.py I have the following function and router:
# blog_post.py
router = APIRouter(
prefix="/blog",
tags=["blog"]
)
def required_functionality():
return {"message": "Learning FastAPI is important"}
within blog_get.py I also have a router and import the required_functionality function:
# blog_get.py
from blog_posts import required_functionality
router = APIRouter(
prefix="/blog",
tags=["blog"]
)
a = required_functionality()
Finally inside main.py im importing both routers:
# main.py
from router import blog_get, blog_posts
app = FastAPI()
app.include_router(blog_get.router)
app.include_router(blog_posts.router)
If I then run main.py I get the following error:
Traceback (most recent call last):
File "C:\Users\UKGC\PycharmProjects\FastAPI\Routers\main.py", line 6, in <module>
from router import blog_get, blog_posts
File "C:\Users\UKGC\PycharmProjects\FastAPI\Routers\router\blog_get.py", line 4, in <module>
from blog_posts import required_functionality
ModuleNotFoundError: No module named 'blog_posts'
If i then change the import in blog_get.py to:
# blog_get.py
from router.blog_posts import required_functionality
router = APIRouter(
prefix="/blog",
tags=["blog"]
)
a = required_functionality()
and run main.py again I have no issues, however, if I run blog_get.py directly I get the following error:
Traceback (most recent call last):
File "C:\Users\UKGC\PycharmProjects\FastAPI\Routers\router\blog_get.py", line 4, in <module>
from router.blog_posts import required_functionality
ModuleNotFoundError: No module named 'router'
How can I get main.py and blog_get.py to both work? I clearly don't seem to understand python imports properly, can someone explain what I'm missing?
I've tried converting router to a package, I've tried relative imports.
You should use relative imports, not absolute imports
# blog_get.py
from .blog_posts import required_functionality
When you execute main.py, there is no directory on your module search path that contains blog_posts.py. But there is a module router.blog_posts, and blog_get (which tries to import blog_posts) is in the package router as well.
To be able to run both scripts and get them to work I needed to append the 'router' to sys.path when importing from main.py. This is a bit of a hacky solution, im still open to other solutions?
# main.py
from router import blog_get, blog_posts
app = FastAPI()
app.include_router(blog_get.router)
app.include_router(blog_posts.router)
and
# blog_get.py
import os
import sys
fpath = os.path.dirname(__file__)
sys.path.append(fpath)
from blog_posts import required_functionality
router = APIRouter(
prefix="/blog",
tags=["blog"]
)
a = required_functionality()
I have a Python API running on Google's Cloud Endpoints framework.
I'm trying to implement a runner for my unittests, using the dev_appserver.
I've copied and adapted what Google is offering about this, just edited a bit to make it run the devappserver all-in-one.
This is what I've done so far:
import argparse
import os
import sys
import unittest
def fixup_paths(path):
"""Adds GAE SDK path to system path and appends it to the google path
if that already exists."""
try:
import google
google.__path__.append("{0}/google".format(path))
except ImportError:
pass
sys.path.insert(0, path)
def main(sdk_path, test_path, test_pattern):
# If the SDK path points to a Google Cloud SDK installation
# then we should alter it to point to the GAE platform location.
if os.path.exists(os.path.join(sdk_path, 'platform/google_appengine')):
sdk_path = os.path.join(sdk_path, 'platform/google_appengine')
# Make sure google.appengine.* modules are importable.
fixup_paths(sdk_path)
# Make sure all bundled third-party packages are available.
import dev_appserver
dev_appserver.fix_sys_path()
# Fix google shits
from google.appengine.tools.devappserver2 import devappserver2
# Start the devappserver
server_instance = devappserver2.DevelopmentServer()
server_instance.start()
# Discover and run tests.
suite = unittest.loader.TestLoader().discover(test_path, test_pattern)
suite_result = unittest.TextTestRunner(verbosity=2).run(suite)
server_instance.stop()
return suite_result
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'sdk_path',
help='The path to the Google App Engine SDK or the Google Cloud SDK.')
parser.add_argument(
'--test-path',
help='The path to look for tests, defaults to the current directory.',
default=os.getcwd())
parser.add_argument(
'--test-pattern',
help='The file pattern for test modules, defaults to *_test.py.',
default='test_*.py')
args = parser.parse_args()
result = main(args.sdk_path, args.test_path, args.test_pattern)
if not result.wasSuccessful():
sys.exit(1)
But i'm struggling with a strange import error.
I've been searching through Github but I was unable to find anything:
Traceback (most recent call last):
File "runner.py", line 65, in <module>
result = main(args.sdk_path, args.test_path, args.test_pattern)
File "runner.py", line 34, in main
from google.appengine.tools.devappserver2 import devappserver2
File "/Users/toto/Libraries/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 27, in <module>
from google.appengine.tools.devappserver2 import api_server
File "/Users/toto/Libraries/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/api_server.py", line 79, in <module>
from google.appengine.tools.devappserver2 import wsgi_server
File "/Users/toto/Libraries/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 48, in <module>
from cherrypy import wsgiserver
File "/usr/local/lib/python2.7/site-packages/cherrypy/__init__.py", line 73, in <module>
from ._cptools import default_toolbox as tools, Tool
File "/usr/local/lib/python2.7/site-packages/cherrypy/_cptools.py", line 33, in <module>
from cherrypy.lib import auth_basic, auth_digest
File "/usr/local/lib/python2.7/site-packages/cherrypy/lib/auth_digest.py", line 27, in <module>
from six.moves.urllib.request import parse_http_list, parse_keqv_list
ImportError: cannot import name parse_http_list
I can't figure out how to fix that import thing :(
The error means that the package six you have installed does not include parse_http_list. Try upgrading the six package:
pip install six==1.14.0
It could be that the parent package then throws an error, then upgrade that package as well.
Getting following error when running python script for web
Traceback (most recent call last):
File "SampleWSTest.py", line 10, in <module>
from web.wsgiserver.ssl_builtin import BuiltinSSLAdapter
File "C:\Python27\lib\site-packages\web\wsgiserver\ssl_builtin.py", line 14, in <module>
from cherrypy import wsgiserver
ImportError: cannot import name wsgiserver
Running on python 2.7.15
import web
import sys
import argparse
import traceback
from web.wsgiserver import CherryPyWSGIServer
from web.wsgiserver.ssl_builtin import BuiltinSSLAdapter
from OpenSSL import SSL
class Healthcheck:
def GET(self):
return 'Yassssssssss !!!!!'
URLS = ('/svc/healthcheck', 'Healthcheck')
CherryPyWSGIServer.ssl_certificate = 'alice.crt'
CherryPyWSGIServer.ssl_private_key = 'alice.key'
if __name__ == '__main__':
CherryPyWSGIServer.ssl_adapter = BuiltinSSLAdapter(CherryPyWSGIServer.ssl_certificate, CherryPyWSGIServer.ssl_private_key)
CherryPyWSGIServer.ssl_adapter.context = SSL.Context(SSL.SSLv23_METHOD)
CherryPyWSGIServer.ssl_adapter.context.set_options(SSL.OP_NO_SSLv3)
CherryPyWSGIServer.ssl_adapter.context.use_certificate_file(CherryPyWSGIServer.ssl_certificate)
CherryPyWSGIServer.ssl_adapter.context.use_privatekey_file(CherryPyWSGIServer.ssl_private_key)
app = web.application(URLS, globals())
app.run()
This started failing after we had to disable ssl 2 and ssl 3 so had to add ssl_adapter but BuiltinSSLAdapter fails with the import.
If any other alternatives please suggest. Basically want to disable ssl 2 and ssl 3 previously we didnt have from web.wsgiserver.ssl_builtin import BuiltinSSLAdapter
from OpenSSL import SSL
CherryPy moved wsgiserver to cheroot in 2017.
http://docs.cherrypy.dev/en/latest/history.html#v9-0-0
So now you need to import it like this:
from cheroot.wsgi import Server as CherryPyWSGIServer
or rename it throughout.
ive been trying to run scrapy from a python script file because i need to get the data and save it into my db. but when i run it with scrapy command
scrapy crawl argos
the script runs fine
but when im trying to run it with a script, following this link
http://doc.scrapy.org/en/latest/topics/practices.html#run-scrapy-from-a-script
i get this error
$ python pricewatch/pricewatch.py update
Traceback (most recent call last):
File "pricewatch/pricewatch.py", line 39, in <module>
main()
File "pricewatch/pricewatch.py", line 31, in main
update()
File "pricewatch/pricewatch.py", line 24, in update
setup_crawler("argos.co.uk")
File "pricewatch/pricewatch.py", line 13, in setup_crawler
settings = get_project_settings()
File "/Library/Python/2.7/site-packages/Scrapy-0.22.2-py2.7.egg/scrapy/utils/project.py", line 58, in get_project_settings
settings_module = import_module(settings_module_path)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named settings
i am unable to understand why it doesnt found get_project_setting() but runs fine with scrapy command on terminal
here is the screen shot of my project
here is the pricewatch.py code:
import commands
import sys
from database import DBInstance
from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy import log
from spiders.argosspider import ArgosSpider
from scrapy.utils.project import get_project_settings
import settings
def setup_crawler(domain):
spider = ArgosSpider(domain=domain)
settings = get_project_settings()
crawler = Crawler(settings)
crawler.configure()
crawler.crawl(spider)
crawler.start()
def update():
#print "Enter a product to update:"
#product = raw_input()
#print product
#db = DBInstance()
setup_crawler("argos.co.uk")
log.start()
reactor.run()
def main():
try:
if sys.argv[1] == "update":
update()
elif sys.argv[1] == "database":
#db = DBInstance()
except IndexError:
print "You must select a command from Update, Search, History"
if __name__ =='__main__':
main()
i have fixed it
just need to put pricewatch.py to project's top level directory and then running it solved it
This answer is heavily copied from this answer which I believe answers your question and additionally provides a descent example.
Consider a project with the following structure.
my_project/
main.py # Where we are running scrapy from
scraper/
run_scraper.py #Call from main goes here
scrapy.cfg # deploy configuration file
scraper/ # project's Python module, you'll import your code from here
__init__.py
items.py # project items definition file
pipelines.py # project pipelines file
settings.py # project settings file
spiders/ # a directory where you'll later put your spiders
__init__.py
quotes_spider.py # Contains the QuotesSpider class
Basically, the command
scrapy startproject scraper is executed in the my_project folder, I've added a run_scraper.py file to the outer scraper folder, a main.py file to my root folder, and quotes_spider.py to the spiders folder.
My main file:
from scraper.run_scraper import Scraper
scraper = Scraper()
scraper.run_spiders()
My run_scraper.py file:
from scraper.scraper.spiders.quotes_spider import QuotesSpider
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
import os
class Scraper:
def __init__(self):
settings_file_path = 'scraper.scraper.settings' # The path seen from root, ie. from main.py
os.environ.setdefault('SCRAPY_SETTINGS_MODULE', settings_file_path)
self.process = CrawlerProcess(get_project_settings())
self.spiders = QuotesSpider # The spider you want to crawl
def run_spiders(self):
self.process.crawl(self.spider)
self.process.start() # the script will block here until the crawling is finished
Also, note that the settings might require a look-over, since the path needs to be according to the root folder (my_project, not scraper).
So in my case:
SPIDER_MODULES = ['scraper.scraper.spiders']
NEWSPIDER_MODULE = 'scraper.scraper.spiders'
etc...
I have installed pysnmp-4.x.I am getting following error during running a SNMP program.
I am using pysnmpSE 3.5.2 now but getting same error. I found that pysnmpSE doesn't hav v4 module. I was suggested that following error should resolved if pySNMP SE 3.x is used.
Traceback (most recent call last):
File "C:\Documents and Settings\ggne0622\Desktop\Python\google-python-exercises\babynames\SimpleAgent.py", line 18, in <module>
from twistedsnmp import agent, agentprotocol, bisectoidstore
File "C:\Python27\Lib\site-packages\twistedsnmp\agent.py", line 4, in <module>
from twistedsnmp import datatypes
File "C:\Python27\Lib\site-packages\twistedsnmp\datatypes.py", line 7, in <module>
from twistedsnmp.pysnmpproto import v2c,v1
File "C:\Python27\Lib\site-packages\twistedsnmp\pysnmpproto.py", line 13, in <module>
from pysnmp.v4.proto.omni import v2c,v1, error, rfc1157, rfc1905
ImportError: No module named v4.proto.omni
Code:
#!/usr/bin/env python
from twisted.internet.iocpreactor import reactor
from twisted.internet import error as twisted_error
from twistedsnmp import agent, agentprotocol, bisectoidstore
#from twisted.internet import interfaces
try:
from twistedsnmp import bsdoidstore
except ImportError:
import warnings
warnings.warn( """No BSDDB OID Storage available for testing""" )
bsdoidstore = None
def createAgent( oids ):
ports = [161]+range(20000,25000)
for port in ports:
try:
`agentObject = reactor.IOCPReactor.listenUDP(port,` `agentprotocol.AgentProtocol(snmpVersion = 'v2c',agent = agent.Agent(dataStore =` `bisectoidstore.BisectOIDStore(OIDs = oids,),),),)`
`except twisted_error.CannotListenError:`
`pass`
`else:`
`return agentObject, port`
testingOIDs = {
'.1.3.6.1.2.1.1.1.0': 'Some tool out in the field',
'.1.3.6.1.2.1.1.2.0': '.1.3.6.1.4.1.88.3.1',
'.1.3.6.1.2.1.1.3.0': 558566090,
'.1.3.6.1.2.1.1.4.0': "support#somewhere.ca",
'.1.3.6.1.2.1.1.5.0': "NameOfSystem",
'.1.3.6.1.2.1.1.6.0': "SomeHeadEnd, West Hinterlands, Canada",
}
def main(oids=testingOIDs):
agent, port = createAgent( oids )
if __name__ == "__main__":
reactor.IOCPReactor.callWhenRunning( main )
reactor.IOCPReactor.run()
TwistedSNMP does not seem to be designed to work with PySNMP 4.x. Thus you should either use PySNMP 3.x / PySNMP SE or switch to PySNMP 4.x which has its own Twisted binding.