语法错误我不明白为什么会这样

时间:2018-07-27 19:18:37

标签: 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.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()

所以就这样) 但现在我收到一条错误消息:

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'

1 个答案:

答案 0 :(得分:0)

只需解决代码最后两行中的缩进问题

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