首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >http.server在windows上工作,但在debian上不起作用。

http.server在windows上工作,但在debian上不起作用。
EN

Stack Overflow用户
提问于 2018-09-10 11:42:54
回答 1查看 115关注 0票数 0

所以我对python比较陌生,但我想我学得很快。为了与我的一个程序进行通信,我制作了一个简单的one服务器:

代码语言:javascript
复制
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import os
import time

class S(BaseHTTPRequestHandler):
    class user():
        def __init__(self, name, hash):
            self.name = name;
            self.hash = hash;
        def terminate(self):
            pass;
    def _set_headers(self, res=200, type="text/html", loc=False):
        self.send_response(res)
        if loc:
            self.send_header('Location', loc)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers(200)
        self.wfile.write(b"<tt><h1>UGame-Server.</h1></tt><hr>Incorrect Method.")

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        if self.headers.get('Content-Length'):
            data = json.loads(self.rfile.read(int(self.headers.get('Content-Length'))));
        else:
            data = {};
        datas = json.dumps(data);
        if self.path == "/":
            self._set_headers(200, "text/html", "index");
            self.path = "/index";
        else:
            self._set_headers(200, "text/html");
        path = self.path[1:];
        print(path);
        import index as serverfile;
        auth=True;
        if path[:6]=="secure":
            auth=False;
            os.chdir("axs");
            if "hash" in data:
                if os.path.exists(data["hash"]+".txt") and time.time()-os.path.getmtime(data["hash"]+".txt")<43200:
                    auth=True;
                    print("Secure access from account "+data["name"]+" verified.");
                    data["user"]=self.user(data["name"],data["hash"]);
                #   del data["name"];
            else:
                if self.client_address[0]=="127.0.0.1":
                    auth=True;
                    data["user"]=self.user("admin","fake");
                    data["name"]="admin";
            os.chdir("..");
            if auth:
                psfr = open("serverdata.posdata.json", "r" );
                posdata = json.loads(psfr.read());
                psfr.close();
                if data["name"] in posdata:
                    data["user"].x = posdata[data["name"]]["x"];
                    data["user"].y = posdata[data["name"]]["y"];
                    data["user"].z = posdata[data["name"]]["z"];
                    data["user"].world = posdata[data["name"]]["world"];
        if auth:
        #   try:
            exec("global response; response = serverfile."+path+"(data);");
        #   except AttributeError:  
        #       exec('global response; response = "Method not found.";');
        else:
            exec('global response; response = "Authentification Failed.";');
        print("Sending response: "+response);
        self.wfile.write(bytes(response, 'utf8'));


def run(server_class=HTTPServer, handler_class=S, port=1103):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print('Starting httpd...');
    httpd.serve_forever()

if __name__ == "__main__":
    from sys import argv

    if len(argv) == 2:
        run(port=int(argv[1]))
    else:
        run()

现在的问题是,这个东西在Windows上运行的非常好,并且做了它应该做的事情。但是一旦涉及到Debian (我的VPS在上面运行的操作系统--这是它最初的目的),它就在第一行立即崩溃。它似乎无法导入http.server,因为来自http.client内部代码的数百语法错误。

例如,第一个错误如下:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/var/www/html/ugame-server/server.py", line 1, in <module>
    from http.server import BaseHTTPRequestHandler, HTTPServer
  File "http/server.py", line 92, in <module>
    import http.client
  File "http/client.py", line 144
    _is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch
                                                         ^
SyntaxError: invalid syntax

我已经检查了所有可用的更新,我的系统,python和所有模块都是最新的。

  • 显然我弄错了。

窗口上没有出现任何一个错误。我也不明白。我们很感激你的帮助。

编辑:更新Debian修复了一些错误,但是出现了新的错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/var/www/html/ugame-server/server.py", line 1, in <module>
    from http.server import BaseHTTPRequestHandler, HTTPServer
  File "/var/www/html/ugame-server/http/server.py", line 92, in <module>
    import http.client
  File "/var/www/html/ugame-server/http/client.py", line 1063
    chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \
                                ^
SyntaxError: invalid syntax
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-10 11:51:03

re.compile(rb'[^:\s][^:\r\n]*'在Python2中是无效的语法,所以看看您在启动服务器时实际使用的版本

作为每秒的错误,f-字符串可以从Python3.6:https://realpython.com/python-f-strings/中访问启动。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52257044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档