Twisted throws & ldquo; Can only bypass bytes on Python 2 & rdquo; witho...
source link: https://www.codesd.com/item/twisted-throws-can-only-bypass-bytes-on-python-2-without-reason.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Twisted throws & ldquo; Can only bypass bytes on Python 2 & rdquo; without reason
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. :/
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)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK