pythonanywhere Flask 应用程序中的连接被拒绝
Connection refused in pythonanywhere flask app
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
它说应用程序有错误或服务器过载。现在,这只是从一个笑话网站中提取一些文本。因此,该站点不太可能拒绝连接。有什么想法吗?
被拒绝的连接是 Selenium 的内部连接 -- 它启动 Firefox 并附加一个小型服务器以控制它,然后通过套接字连接与其通信。原因是您在“finally”块内关闭了浏览器。在与“finally”关联的 try 块中,您定义了视图函数 get_jokes
,但是当稍后实际调用它时,try/finally 块已经退出,因此没有浏览器 运行 -- 因此与它的 Selenium 连接失败。
您应该将完整的 try/finally 块 放在 视图函数中。
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
它说应用程序有错误或服务器过载。现在,这只是从一个笑话网站中提取一些文本。因此,该站点不太可能拒绝连接。有什么想法吗?
被拒绝的连接是 Selenium 的内部连接 -- 它启动 Firefox 并附加一个小型服务器以控制它,然后通过套接字连接与其通信。原因是您在“finally”块内关闭了浏览器。在与“finally”关联的 try 块中,您定义了视图函数 get_jokes
,但是当稍后实际调用它时,try/finally 块已经退出,因此没有浏览器 运行 -- 因此与它的 Selenium 连接失败。
您应该将完整的 try/finally 块 放在 视图函数中。