0

I am using twisted as my webserver. I am delivering normal text sites and binary downloads with this setup.

I am using the exact same setup on 6 machines. Only difference is 3 are running Debian and the other 3 are running Ubuntu.

On two out of my three Ubuntu machines I am getting this error:

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/twisted/protocols/basic.py", line 571, in dataReceived
    why = self.lineReceived(line)
  File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 1655, in lineReceived
    self.allContentReceived()
  File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 1730, in allContentReceived
    req.requestReceived(command, path, version)
  File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 826, in requestReceived
    self.process()
--- <exception caught here> ---  
  File "/usr/lib/python2.7/dist-packages/twisted/web/server.py", line 189, in process
    self.render(resrc)
  File "/usr/lib/python2.7/dist-packages/twisted/web/server.py", line 238, in render
    body = resrc.render(self)
  File "/usr/lib/python2.7/dist-packages/twisted/web/resource.py", line 250, in render
    return m(request)
  File "/usr/lib/python2.7/dist-packages/twisted/web/static.py", line 631, in render_GET
    producer.start()
  File "/usr/lib/python2.7/dist-packages/twisted/web/static.py", line 710, in start
    self.request.registerProducer(self, False)
  File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 872, in registerProducer
    self.transport.registerProducer(producer, streaming)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/_newtls.py", line 233, in registerProducer
    FileDescriptor.registerProducer(self, producer, streaming)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/abstract.py", line 112, in registerProducer
    producer.resumeProducing()
  File "/usr/lib/python2.7/dist-packages/twisted/web/static.py", line 720, in resumeProducing
    self.request.write(data)
  File "/usr/lib/python2.7/dist-packages/twisted/web/server.py", line 217, in write
    http.Request.write(self, data)
  File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 1001, in write
    value = networkString('%s' % (value,))
  File "/usr/lib/python2.7/dist-packages/twisted/python/compat.py", line 364, in networkString
    raise TypeError("Can only pass-through bytes on Python 2")
exceptions.TypeError: Can only pass-through bytes on Python 2
Unhandled Error
Traceback (most recent call last):
Failure: exceptions.RuntimeError: Producer was not unregistered for /file/10983801

The other runs just fine.

Python version - on all three ubuntu servers - is: ii python2.7 2.7.6-8 amd64

I haven't updated python recently nor did I change something within my codebase. I also tried rebooting -> no success.

I would really appreciate some hints on that. Googeling only hinted me to this: Running Portia (scrapy) on Windows But since I am running 2.7.6 and Linux this shouldn't apply to my situation.

EDIT appending the actual code:

class PyQueueFile(Resource):

    def __init__(self):
        Resource.__init__(self)
        self.ipcTalker = talker.Talker()

    def getChild(self, convert_id, request):
        """
        :param request: The http Request
        :type request: twisted.web.http.Request
        """
        try:
            db = database.Database()
            video = db.getVideo(convert_id)

            request.setHeader("Content-Disposition",
                              "attachment; filename=\"" + os.path.basename(video['title'] + "." + video['format']) + "\"")
            request.setHeader("Content-type", "application/force-download")

            fileResponse = File(video['path'])
        except TypeError:
            return Page404()

        return fileResponse


def fireup():
    try:

        myconfig = config.Config()

        root = Resource()
        root.putChild("file", PyQueueFile())

        factory = Site(root)
        reactor.listenTCP(myconfig.webPort, factory, 100, myconfig.webIp)
        reactor.run()
    except (KeyboardInterrupt, SystemExit):
        reactor.stopListening()

EDIT 2: I have also tried to install twisted via pip. Same problem. :/

2
  • 2
    You have a program that encounters an error. Ok, that's a start. Care to share the program? Or better, sscce.org Commented Aug 30, 2014 at 12:20
  • I didn't append any code of mine, because none of my code does appear in the traceback. Anyway, I have appended the code that should be causing it.
    – isset
    Commented Aug 30, 2014 at 12:25

2 Answers 2

3

Check the chapter "unicode filenames" in https://docs.python.org/2/howto/unicode.html explaing this:

>>> import os
>>> os.path.basename(u'/a/b/c')
u'c'
>>> os.path.basename('/a/b/c')
'c'

Anyway your fix will fail for non-ascii characters in the filename, it should be URL-encoded (urllib.urlencode)

1
  • Thanks for you suggestion! In my special case it can't happen, because I am filtering all non-ascii characters in another part of my app. I will accept your answer, since you gave me the hint :)
    – isset
    Commented Aug 30, 2014 at 23:45
1

With the help of the mailing list I could fix the problem.

It was caused by a unicode string.

Changing this line:

"attachment; filename=\"" + os.path.basename(video['title'] + "." + video['format']) + "\"")

to this:

"attachment; filename=\"" + str(os.path.basename(video['title']) + "." + video['format']) + "\"")

solves the problem. But I still have no idea why this didn't happen on all platforms?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.