Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listen on unix socket #325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ bind_address
The IP address where the website and json webservices will listen.
Defaults to ``127.0.0.1`` (localhost)

unix_socket_path
----------------

The filesystem path for a unix socket where the HTTP JSON API will listen.
Example: ``/var/run/scrapyd/http-api.socket``
File permissions: 0660

.. _max_proc:

max_proc
Expand Down
2 changes: 2 additions & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Added
- Jobs website shortcut to cancel a job using the cancel.json webservice.
- Make project argument to listjobs.json optional,
so that we can easily query for all jobs.
- Scrapyd can now listen on a unix socket.
This allows securing the application from users on the same system.

Removed
~~~~~~~
Expand Down
24 changes: 18 additions & 6 deletions scrapyd/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from twisted.application.service import Application
from twisted.application.internet import TimerService, TCPServer
from twisted.application.internet import TimerService, TCPServer, UNIXServer
from twisted.web import server
from twisted.python import log

Expand All @@ -16,6 +18,9 @@ def application(config):
app = Application("Scrapyd")
http_port = config.getint('http_port', 6800)
bind_address = config.get('bind_address', '127.0.0.1')
uds_path = config.get('unix_socket_path', '')
uds_path = uds_path and os.path.abspath(uds_path)

poll_interval = config.getfloat('poll_interval', 5)

poller = QueuePoller(config)
Expand All @@ -33,15 +38,22 @@ def application(config):
launcher = laucls(config, app)

webpath = config.get('webroot', 'scrapyd.website.Root')
webcls = load_object(webpath)
website = server.Site(load_object(webpath)(config, app))

timer = TimerService(poll_interval, poller.poll)
webservice = TCPServer(http_port, server.Site(webcls(config, app)), interface=bind_address)
log.msg(format="Scrapyd web console available at http://%(bind_address)s:%(http_port)s/",
bind_address=bind_address, http_port=http_port)

launcher.setServiceParent(app)
timer.setServiceParent(app)
webservice.setServiceParent(app)

if http_port:
webservice = TCPServer(http_port, website, interface=bind_address)
log.msg(format="Scrapyd web console available at http://%(bind_address)s:%(http_port)s/",
http_port=http_port, bind_address=bind_address)
webservice.setServiceParent(app)
if uds_path:
webservice = UNIXServer(uds_path, website, mode=0o660)
log.msg(format=u"Scrapyd web console available at http+unix://%(uds_path)s",
uds_path=uds_path)
webservice.setServiceParent(app)
Copy link
Contributor

@pawelmhm pawelmhm Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not certain if I understand the logic here, is this really meant to start both TCPServer and UnixServer? Or is there some intention to allow to disable and stop TCPServer by setting http_port to zero? If 2nd option, if we want to add option to disable TCP this should be explicitly documented in docs.

Because with this implementation if someone just adds uds_path without removing http_port it will start both of them, unixserver and tcp. Is that expected?


return app