selenium.common.exceptions.WebDriverException: Message: connection refused while launching Firefox browser through Selenium and Python - python-2.7

I am trying this code:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
It opens firefox but does not get the link and shows this message:
Traceback (most recent call last):
File "new.py", line 3, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 245, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
Configuration:
selenium latest version
browser latest version
python 2.7.14
os:kali linux 2.0
geckodriver log:
1527001787163 geckodriver INFO geckodriver 0.20.1
1527001787169 geckodriver INFO Listening on 127.0.0.1:51383
1527001788204 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.IKjgvyUQaThG"
1527001790297 Marionette INFO Listening on port 2828
[

This error message...
selenium.common.exceptions.WebDriverException: Message: connection refused
...implies that WebDriverException which is the base webdriver exception was raised as the driver failed to start its internal server to communicate with the python client.
Though you mentioned about selenium latest version and browser latest version, the exact details would have helped us to diagnose the issue in a much easier way.
Again, though you mentioned the message but I don't see any error in geckodriver log.
Try the following steps :
Pass the Key executable_path along with the Value referring to the absolute path of the GeckoDriver as follows :
from selenium import webdriver
browser = webdriver.Firefox(executable_path='/path/to/geckodriver')
browser.get('http://seleniumhq.org/')
Upgrade Selenium to current levels Version 3.12.0.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your #Test.
Presumably, there was an instance of GeckoDriver and Firefox Browser Client active previously, so always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
You can find a detailed discussion in:
How to fix “WebDriverException: Message: connection refused”?
Selenium Python selenium.common.exceptions.WebDriverException: Message: connection refused using geckodriver and firefox

You can try this code :
from selenium import webdriver
driver = webdriver.Firefox(executable_path = r'you web driver full path')
driver.get("http://seleniumhq.org/")

You need to kill all firefox processes to release the resources.
Add this line to kill.sh
kill $(ps aux | awk '/firefox/ {print $2}')
run
sh kill.sh

Related

How to solve the error with deploying a model in aws sagemaker?

I have to deploy a custom keras model in AWS Sagemaker. I have a created a notebook instance and I have the following files:
AmazonSagemaker-Codeset16
-ann
-nginx.conf
-predictor.py
-serve
-train.py
-wsgi.py
-Dockerfile
I now open the AWS terminal and build the docker image and push the image in the ECR repository. Then I open a new jupyter python notebook and try to fit the model and deploy the same. The training is done correctly but while deploying I get the following error:
"Error hosting endpoint sagemaker-example-2019-10-25-06-11-22-366: Failed. >Reason: The primary container for production variant AllTraffic did not pass >the ping health check. Please check CloudWatch logs for this endpoint..."
When I check the logs, I find the following:
2019/11/11 11:53:32 [crit] 19#19: *3 connect() to unix:/tmp/gunicorn.sock >failed (2: No such file or directory) while connecting to upstream, client: >10.32.0.4, server: , request: "GET /ping HTTP/1.1", upstream: >"http://unix:/tmp/gunicorn.sock:/ping", host: "model.aws.local:8080"
and
Traceback (most recent call last):
File "/usr/local/bin/serve", line 8, in
sys.exit(main())
File "/usr/local/lib/python2.7/dist->packages/sagemaker_containers/cli/serve.py", line 19, in main
server.start(env.ServingEnv().framework_module)
File "/usr/local/lib/python2.7/dist->packages/sagemaker_containers/_server.py", line 107, in start
module_app,
File "/usr/lib/python2.7/subprocess.py", line 711, in init
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
I tried to deploy the same model in AWS Sagemaker with these files in my local computer and the model was deployed successfully but inside AWS, I am facing this problem.
Here is my serve file code:
from __future__ import print_function
import multiprocessing
import os
import signal
import subprocess
import sys
cpu_count = multiprocessing.cpu_count()
model_server_timeout = os.environ.get('MODEL_SERVER_TIMEOUT', 60)
model_server_workers = int(os.environ.get('MODEL_SERVER_WORKERS', cpu_count))
def sigterm_handler(nginx_pid, gunicorn_pid):
try:
os.kill(nginx_pid, signal.SIGQUIT)
except OSError:
pass
try:
os.kill(gunicorn_pid, signal.SIGTERM)
except OSError:
pass
sys.exit(0)
def start_server():
print('Starting the inference server with {} workers.'.format(model_server_workers))
# link the log streams to stdout/err so they will be logged to the container logs
subprocess.check_call(['ln', '-sf', '/dev/stdout', '/var/log/nginx/access.log'])
subprocess.check_call(['ln', '-sf', '/dev/stderr', '/var/log/nginx/error.log'])
nginx = subprocess.Popen(['nginx', '-c', '/opt/ml/code/nginx.conf'])
gunicorn = subprocess.Popen(['gunicorn',
'--timeout', str(model_server_timeout),
'-b', 'unix:/tmp/gunicorn.sock',
'-w', str(model_server_workers),
'wsgi:app'])
signal.signal(signal.SIGTERM, lambda a, b: sigterm_handler(nginx.pid, gunicorn.pid))
# If either subprocess exits, so do we.
pids = set([nginx.pid, gunicorn.pid])
while True:
pid, _ = os.wait()
if pid in pids:
break
sigterm_handler(nginx.pid, gunicorn.pid)
print('Inference server exiting')
# The main routine just invokes the start function.
if __name__ == '__main__':
start_server()
I deploy the model using the following:
predictor = classifier.deploy(1, 'ml.t2.medium', serializer=csv_serializer)
Kindly let me know the mistake I am doing while deploying.
Using Sagemaker script mode can be much simpler than dealing with container and nginx low-level stuff like you're trying to do, have you considered that?
You only need to provide the keras script:
With Script Mode, you can use training scripts similar to those you would use outside SageMaker with SageMaker's prebuilt containers for various deep learning frameworks such TensorFlow, PyTorch, and Apache MXNet.
https://github.com/aws-samples/amazon-sagemaker-script-mode/blob/master/tf-sentiment-script-mode/sentiment-analysis.ipynb
You should ensure that your container can respond to GET /ping requests: https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-inference-code.html#your-algorithms-inference-algo-ping-requests
From the traceback, it looks like the server is failing to start when the container is started within SageMaker. I would look further in the stack trace and see why the server is failing start.
You can also try to run your container locally to debug any issues. SageMaker starts your container using the command 'docker run serve', and so you could run the same command and debug your container. https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-inference-code.html#your-algorithms-inference-code-run-image
you don't have gunicorn installed, that's why the error /tmp/gunicorn.sock >failed (2: No such file or directory), you need to write on Dockerfile pip install gunicorn and apt-get install nginx.

502 error django + nginx + gunicorn - connect() recv()

I've set up a Digital Ocean one-click app (django + nginx + gunicorn are installed and setup https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image). Defaults worked for me, but after I tried to apply code changes via service gunicorn restart I received a 502 error with the following nginx error log line:
connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 178.136.215.70, server: _, request: "GET / HTTP/1.1",upstream: "http://unix:/home/django/gunicorn.socket:/"..
I have looked into similar issues and found that often it's caused by a mistake in ALLOWED_HOSTS, everything is right there, but I also tried to replace 'ip' or 'www.address.com' with single '*' and got a different error:
recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 178.136.215.70, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "192.241.176.184"
EDIT: ok, now it's easier, gunicorn error log is telling us that there is an import error with rest_framework, But it's installed and it's present in Installed app (and spelled right), I'v checked like thousand times so what's really wrong with all that?
File "/usr/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/usr/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/usr/lib/python2.7/dist-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/usr/lib/python2.7/dist-packages/gevent/builtins.py", line 93, in __import__
result = _import(*args, **kwargs)
File "/home/django/django_project/blog/models.py", line 5, in <module>
from rest_framework import serializers
File "/usr/lib/python2.7/dist-packages/gevent/builtins.py", line 93, in __import__
result = _import(*args, **kwargs)
ImportError: No module named rest_framework
(the only issue for now)EDIT2: I have reinstalled the app folder, checked again everything. Now it's telling that ImportError: No module named django_ajax. Python 2.7 , Django 1.8, Gunicorn 19.4.5 (https://github.com/yceruto/django-ajax) despite everything seems to be correct
EDIT: Résponse of pip freeze:
Django==1.8
django-filter==1.0.1
-e git://github.com/yceruto/django-ajax#9c122e68f8e7ca92333a1533fa464ee6da0f65c5#egg=djangoajax
djangorestframework==3.5.3
gunicorn==19.6.0
Markdown==2.6.7
netifaces==0.10.5
pkg-resources==0.0.0
psycopg2==2.6.2
When you do Sudo, the package is installed for the root user and it installed globally. Check this link to understand how sudo pip works. Now that your package is installed globally, it is working.
I still have doubts about which Python is it using. In the error messages, it was trying to access packages under global dist-packages directory. Not from the virtual environment. So, your gunicorn send to be linked to the Python installed globally not the virtual environment one.
Ideally, the errors occurred should point to the dist-packages under the /path/to/virtual_env/virtual_env/lib/python2.7/dist-packages, as this is the python & the library environment that want to use. That seems to be the issue.
If this is the case, can you do the following
DJANGODIR=/path/to/django-project/
DJANGO_SETTINGS_MODULE=django-project.settings
Activate the virtual environment
cd $DJANGODIR
source /path/to/virtualenv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
I think this should solve everything... Please try it out and lemme know if I missed out something
I am not sure, whether it shall be considered as answer, because I don't understand why exactly it works this way. If someone explains properly I'll mark one's answer as right one . So, this line worked for me
sudo -H pip install djangorestframework
Despite packages were reported to be installed they were not.And despite they were visible after pip freeze. So I don't know real reason why just pip install didn't work but seemed to work instead(?), although all permissions are correct, and I tried from both root and other username with same privileges.

Connect to AS400 database with Django (ibm_db_django)

I have created a new project in Django and am following this tutorial to help me pull data from my remote database. Whenever I try to run migrate in manage.py I get the following error:
"C:\Program Files (x86)\JetBrains\PyCharm 2016.2.1\bin\runnerw.exe" C:\Users\ecjohnson\AppData\Local\Programs\Python\Python35-32\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2016.2.1\helpers\pycharm\django_manage.py" migrate U:/incoming_parts_monitor
Traceback (most recent call last):
File "C:\Users\ecjohnson\AppData\Local\Programs\Python\Python35-32\lib\site-packages\ibm_db_dbi.py", line 585, in connect
conn = ibm_db.connect(dsn, '', '', conn_options)
SQLCODE=-30081
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ecjohnson\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
File "C:\Users\ecjohnson\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\base\base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
I've connected to this database before in other .NET web applications so I'm not sure why this is not working.
The sqlcode=-30081 is a generic "unable to connect" condition. Look to the configuration and connection details, on both the requesting and server systems, and review for any messaging about a connection issue on the server side [of possible assist is a review of the Display Log (DSPLOG) for the history (QHST), and then review of the joblog(s) of the job(s) that originated that messaging] as an indication that the communications-level connection was at least completed, but some error after that initial comm connection prevented establishing an actual database connection; e.g. perhaps the prestart job or the subsystem in which the database server jobs operate is not started.

pip failing to install packages behind a proxy

Am new to python. As part of my project setup I tried to install soem python packages using pip as
pip install --proxy="uname:pwd#ip:port" pymssql
But am getting following log in pip.log
page http://pypi.python.org/simple/pymssql
Could not fetch URL http://pypi.python.org/simple/pymssql: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>
Will skip URL http://pypi.python.org/simple/pymssql when looking for download links for pymssql
Getting page http://pypi.python.org/simple/
Could not fetch URL http://pypi.python.org/simple/: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>
Will skip URL http://pypi.python.org/simple/ when looking for download links for pymssql
Cannot fetch index base URL http://pypi.python.org/simple/
URLs to search for versions for pymssql:
* http://pypi.python.org/simple/pymssql/
Getting page http://pypi.python.org/simple/pymssql/
Could not fetch URL http://pypi.python.org/simple/pymssql/: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>
Will skip URL http://pypi.python.org/simple/pymssql/ when looking for download links for pymssql
Could not find any downloads that satisfy the requirement pymssql
No distributions at all found for pymssql
Exception information:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\pip\basecommand.py", line 126, in main
self.run(options, args)
File "C:\Python27\lib\site-packages\pip\commands\install.py", line 223, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "C:\Python27\lib\site-packages\pip\req.py", line 954, in prepare_files
url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
File "C:\Python27\lib\site-packages\pip\index.py", line 152, in find_requirement
raise DistributionNotFound('No distributions at all found for %s' % req)
DistributionNotFound: No distributions at all found for pymssql
Any help will be appreciated.
just set this 2 variables on your environment (linux or windows):
http_proxy http://user:passarod#proxy:port
https_proxy http://user:passarod#proxy:port

Django send_mail results in Error 60, Operation Timed Out on Mac OSX

Running into a very stange error. I'm running Django on my Mac OSX and when I tried to send an email from my application it hangs and gives me this error: "Error 60, Operation Timed Out". Exception Location: ...python2.6/socket.py in create_connection
Any ideas?
Are you running a local OSX SMTP server? if not then may I suggest this script which when used during development will dump any locally sent mail message to a file:
http://www.djangosnippets.org/snippets/96/