2014-08-30 22 views
0

我正在使用twisted作爲我的web服務器。我正在使用此設置提供正常的文本網站和二進制文件下載。Twisted throws「只能在Python 2上傳遞字節」無故原因

我在6臺機器上使用完全相同的設置。唯一不同的是3正在運行Debian,另外3個正在運行Ubuntu。

在兩頭在外我的三個Ubuntu機器的我收到此錯誤:

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 

其他運行就好了。

Python版本 - 所有這三個Ubuntu的服務器上 - 是:二python2.7 2.7.6-8 AMD64

我還沒有更新蟒蛇最近我也沒有改變我的代碼庫中的東西。我也嘗試重新啓動 - >沒有成功。

我真的很感激這方面的一些提示。 Googeling只暗示了我這個: Running Portia (scrapy) on Windows 但是由於我運行2.7.6和Linux,這不適用於我的情況。

編輯附加的實際代碼:

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() 

編輯2: 我也試圖通過畫中畫扭曲安裝。同樣的問題。 :/

+2

你有一個遇到錯誤的程序。好的,這是一個開始。謹慎分享該計劃?或者更好,http://sscce.org/ – 2014-08-30 12:20:35

+0

我沒有附加任何我的代碼,因爲我的代碼沒有出現在回溯中。無論如何,我已經附加了應該引起它的代碼。 – isset 2014-08-30 12:25:34

回答

3

檢查章「Unicode文件名」中https://docs.python.org/2/howto/unicode.html explaing這樣的:

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

反正你的修補程序將在文件名非ASCII字符失敗,它應該是URL編碼(urllib.urlencode)

+0

感謝您的建議!在我的特殊情況下,它不會發生,因爲我正在過濾應用程序另一部分中的所有非ascii字符。我會接受你的回答,因爲你給了我提示:) – isset 2014-08-30 23:45:07

1

在郵件列表的幫助下,我可以解決這個問題。

這是由unicode字符串引起的。

更改該行:

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

這樣:

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

解決了這個問題。但我仍然不知道爲什麼這不會在所有平臺上發生?

相關問題