0
votes
from selenium import webdriver
from flask import Flask, render_template
import random
from pyvirtualdisplay import Display
from selenium.webdriver.firefox.options import Options as FirefoxOptions

app = Flask(__name__)
with Display():
     options = FirefoxOptions()
     options.add_argument('--headless')
     driver = webdriver.Firefox(firefox_options=options)
     driver.implicitly_wait(10)
     t_file = '/home/parshuram/mysite/static/links'
     target = open(t_file).read().splitlines()
try:
    @app.route('/')
    def get_jokes():
        driver.get(random.choice(target))
        jokes = driver.find_elements_by_tag_name('p')
        mylist = []
        for joke in jokes:
            mylist.append(joke.text)
            return render_template('/home/parshuram/mysite/Template/joke.html', s=mylist)

    if __name__ == "__main__":
        app.run()
finally:
    driver.quit()

2020-10-05 17:59:33,327: Exception on / [GET] Traceback (most recent call last): File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functionsrule.endpoint File "/home/parshuram/mysite/sick.py", line 18, in get_jokes driver.get(random.choice(target)) File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 248, in get self.execute(Command.GET, {'url': url}) File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 234, in execute response = self.command_executor.execute(driver_command, params) File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 401, in execute return self._request(command_info[0], url, body=data) File "/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 432, in _request self._conn.request(method, parsed_url.path, body, headers) File "/usr/lib/python3.8/http/client.py", line 1230, in request self._send_request(method, url, body, headers, encode_chunked) File "/usr/lib/python3.8/http/client.py", line 1276, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/usr/lib/python3.8/http/client.py", line 1225, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/usr/lib/python3.8/http/client.py", line 1004, in _send_output self.send(msg) File "/usr/lib/python3.8/http/client.py", line 944, in send self.connect() File "/usr/lib/python3.8/http/client.py", line 915, in connect self.sock = self._create_connection( File "/usr/lib/python3.8/socket.py", line 807, in create_connection raise err File "/usr/lib/python3.8/socket.py", line 796, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused

It says either the application has an error or the server is overloaded. Now, this is just pulling some text from a joke site. So, it is unlikely that the site would be refusing connections. Any ideas?

1

1 Answers

2
votes

The connection that is being refused is internal to Selenium -- it starts up Firefox with a small server attached to it in order to control it, and then communicates with it over a socket connection. The cause is that you are closing down your browser inside that "finally" block. Inside the try block associated with that "finally", you define your view function get_jokes, but when it is actually called later on, the try/finally block has already exited, so there is no browser running -- so the Selenium connection to it fails.

You should put the complete try/finally block inside the view function.