所以我对python比较陌生,但我想我学得很快。为了与我的一个程序进行通信,我制作了一个简单的one服务器:
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内部代码的数百语法错误。
例如,第一个错误如下:
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修复了一些错误,但是出现了新的错误:
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发布于 2018-09-10 11:51:03
re.compile(rb'[^:\s][^:\r\n]*'在Python2中是无效的语法,所以看看您在启动服务器时实际使用的版本
作为每秒的错误,f-字符串可以从Python3.6:https://realpython.com/python-f-strings/中访问启动。
https://stackoverflow.com/questions/52257044
复制相似问题