Python 语法错误我不明白为什么会发生这种情况
因此,我目前正在用python从头开始构建一个博客类型的系统,我目前正在尝试运行我的博客创建程序,我不断收到一个错误消息Python 语法错误我不明白为什么会发生这种情况,python,sockets,syntax-error,Python,Sockets,Syntax Error,因此,我目前正在用python从头开始构建一个博客类型的系统,我目前正在尝试运行我的博客创建程序,我不断收到一个错误消息 File "create.py", line 55 cc.sendall(rp) ^ SyntaxError: invalid syntax 这是我的全部代码,提前感谢所有回复。 导入套接字,操作系统 HOST, PORT = '', 8080 ls = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ls.setso
File "create.py", line 55
cc.sendall(rp)
^
SyntaxError: invalid syntax
这是我的全部代码,提前感谢所有回复。
导入套接字,操作系统
HOST, PORT = '', 8080
ls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ls.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ls.bind((HOST, PORT))
ls.listen(1)
while True:
cc, ca = ls.accept()
rq = cc.recv(1024)
rp = """\
HTTP/1.1 200 OK
<html>
Post succesfully posted! Return to <a href='http://192.168.0.228/'>the
homepage</a>?
</html>
"""
substrings = []
substrings.insert(0, rq[rq.find("GET ")+3:rq.find(" HTTP/1.1")])
if substrings[0] != ' /favicon.ico':
substrings.insert(1, rq[12:rq.find("&")])
substrings.insert(2, rq[rq.find("&")+7:len(substrings[0])+3])
f = open("posts/{}.html".format(substrings[1]), "w+")
f.write("""\
<html>
<title>
{}
</title>
<script>
function createcomment(){
url = {}
comment = prompt('What is your comment?');
newurl = "http://192.168.0.228:8888/name?=" + url + "&comment?=" + comment;
document.location.href = newurl;
}
</script>
<body>
<p>
{}
</p>
<button onclick="createcomment()">Comment</button>
</body>
</html>
""".format(substrings[1], substrings[1], substrings[2])
cc.sendall(rp)
cc.close()
主机,端口='',8080
ls=socket.socket(socket.AF\u INET,socket.SOCK\u流)
ls.setsockopt(socket.SOL_socket,socket.SO_REUSEADDR,1)
ls.bind((主机、端口))
听一听(1)
尽管如此:
cc,ca=ls.accept()
rq=cc.recv(1024)
rp=”“”\
HTTP/1.1200ok
发布成功!返回到?
"""
子字符串=[]
插入(0,rq[rq.find(“GET”)+3:rq.find(“HTTP/1.1”))
如果子字符串[0]!='/favicon.ico':
插入(1,rq[12:rq.find(&”))
子字符串.insert(2,rq[rq.find(&”)+7:len(子字符串[0])+3])
f=open(“posts/{}.html”.format(子字符串[1]),“w+”)
f、 写(“”)\
{}
函数createcomment(){
url={}
comment=prompt('您的评论是什么?');
新网址=”http://192.168.0.228:8888/name?=“+url+”&注释?=”+注释;
document.location.href=newurl;
}
{}
评论
“”格式(子字符串[1]、子字符串[1]、子字符串[2])
抄送sendall(rp)
cc.close()
就是这样)
但现在我得到了一个错误,说:
Traceback (most recent call last):
File "create.py", line 54, in <module>
""".format(substrings[1], substrings[1], substrings[2]))
KeyError: '\n\t\turl = {}\n\t\tcomment = prompt(\'What is your comment?\');\n\t\tnewurl = "http'
回溯(最近一次呼叫最后一次):
文件“create.py”,第54行,在
“”格式(子字符串[1]、子字符串[1]、子字符串[2]))
KeyError:“\n\t\turl={}\n\t\t建议=prompt(\'What's your comment?\”);\n\t\tnewurl=“http”
只需解决代码最后两行中的缩进问题
HOST, PORT = '', 8080
ls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ls.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ls.bind((HOST, PORT))
ls.listen(1)
while True:
cc, ca = ls.accept()
rq = cc.recv(1024)
rp = """\
HTTP/1.1 200 OK
<html>
Post succesfully posted! Return to <a href='http://192.168.0.228/'>the
homepage</a>?
</html>
"""
substrings = []
substrings.insert(0, rq[rq.find("GET ")+3:rq.find(" HTTP/1.1")])
if substrings[0] != ' /favicon.ico':
substrings.insert(1, rq[12:rq.find("&")])
substrings.insert(2, rq[rq.find("&")+7:len(substrings[0])+3])
f = open("posts/{}.html".format(substrings[1]), "w+")
f.write("""\
<html>
<title>
{}
</title>
<script>
function createcomment(){
url = {}
comment = prompt('What is your comment?');
newurl = "http://192.168.0.228:8888/name?=" + url + "&comment?=" + comment;
document.location.href = newurl;
}
</script>
<body>
<p>
{}
</p>
<button onclick="createcomment()">Comment</button>
</body>
</html>
""".format(substrings[1], substrings[1], substrings[2])
cc.sendall(rp)
cc.close()
主机,端口='',8080
ls=socket.socket(socket.AF\u INET,socket.SOCK\u流)
ls.setsockopt(socket.SOL_socket,socket.SO_REUSEADDR,1)
ls.bind((主机、端口))
听一听(1)
尽管如此:
cc,ca=ls.accept()
rq=cc.recv(1024)
rp=”“”\
HTTP/1.1200ok
发布成功!返回到?
"""
子字符串=[]
插入(0,rq[rq.find(“GET”)+3:rq.find(“HTTP/1.1”))
如果子字符串[0]!='/favicon.ico':
插入(1,rq[12:rq.find(&”))
子字符串.insert(2,rq[rq.find(&”)+7:len(子字符串[0])+3])
f=open(“posts/{}.html”.format(子字符串[1]),“w+”)
f、 写(“”)\
{}
函数createcomment(){
url={}
comment=prompt('您的评论是什么?');
新网址=”http://192.168.0.228:8888/name?=“+url+”&注释?=”+注释;
document.location.href=newurl;
}
{}
评论
“”格式(子字符串[1]、子字符串[1]、子字符串[2])
抄送sendall(rp)
cc.close()
它看起来太缩进了,尽管我预计会出现与此不同的错误。是的,谢谢,在此之前我尝试过处理缩进,正如您所说,我预计会出现与此不同的错误。您缺少写入函数的结束符)
.format(子字符串[1]、子字符串[1]、子字符串[2])
,而且cc.sendall太缩进。与无法解释的语法错误的常见情况一样,它在前一行缺少一个右括号(从前面的20行中关闭f.write(…
)。如果你的编辑器不能显示匹配的括号/方括号等。-扔掉它,并获得一个真正适合编程的。谢谢大家,我的编辑器(vim确实显示了这些内容)我只是没有注意到它。