python-http.server在Windows上有效,但在debian上无效

时间:2018-09-10 11:42:55

标签: python http debian

所以我对python来说还比较陌生,但是我认为我是一个快速学习者。 为了与我的一个程序进行通信,我制作了一个简单的网络服务器:

def find_it(seq)
  seq.group_by{ |item| item }
     .find{ |k, v| v.size.odd? }
     .first
end

现在的事情是,这个事情在Windows上可以很好地运行,并且完全可以完成它的工作。但是,一旦涉及Debian,我的VPS最初就在运行我的VPS操作系统,它立即在第一行崩溃。由于 http.client 的内部代码,语法错误数百,因此它似乎无法导入 http.server

例如,第一个错误如下:

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

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

  • 显然我弄错了。

没有一个错误出现在窗口中。我不明白。感谢您的帮助。

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

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

1 个答案:

答案 0 :(得分:0)

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

每秒出现错误,从Python 3.6开始可以访问f字符串: https://realpython.com/python-f-strings/