Luk*_*uke 1 twisted twisted.web python-3.x
代码在这里.
from twisted.web.static import File
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import ssl, reactor
from twisted.python.modules import getModule
import secure_aes
import urllib.parse
import cgi
import json
import os
import hashlib
import coserver
import base64
import sim
if not os.path.exists(os.path.join(os.getcwd(),'images')):
os.mkdir(os.path.join(os.getcwd(),'images'))
with open ('form.html','r') as f:
fillout_form = f.read()
with open ('image.html','r') as f:
image_output = f.read()
port = 80#int(os.environ.get('PORT', 17995))
class FormPage(Resource):
#isLeaf = True
def getChild(self, name, request):
print('GC')
if name == '':
return self
return Resource.getChild(self, name, request)
def render_GET(self, request):
print(request)
#do stuff and return stuff
root = FormPage()
root.putChild('rcs', File("./images"))
#factory = Site(FormPage())
factory = Site(root)
reactor.listenTCP(port, factory)
reactor.run()
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我做到root.putChild了最后的事情,期待当我到达时,http://site/rcs我得到一个目录列表的内容./images但当然不会发生.我错过了什么?我从这里尝试了许多建议.另外这个不起作用,因为它只是提供静态文件.无论是否指定了putChild,它都会一直进入getChild.
在Python 3上,一个简单的字符串文字就像"rcs"是一个unicode字符串(Python 3称之为"str",但我称之为"unicode"以避免歧义).
但是,twisted.web.resource.Resource.putChild需要一个字节字符串作为其第一个参数.相反,当给予unicode时,它行为不当.使您的路径段成为字节字符串(例如b"rcs"),服务器在Python 3上的表现会更好.
| 归档时间: |
|
| 查看次数: |
406 次 |
| 最近记录: |