Python socket [WinError 10057]?

时间:2015-03-05 12:04:02

标签: python-3.x

当我运行此代码时,我得到了[WinError 10057]。当我浏览到localhost时,我不知道为什么它会崩溃:8081,因为相同的代码正在我的朋友机器上运行...

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 8081))
sock.listen(2)
conn, addr = sock.accept()

ans = conn.recv(1024).decode("ascii")  
sock.sendall(bytearray("HTTP/1.1 200 ok\n", "ascii"))
sock.sendall(bytearray("\n", "ascii"))
sock.sendall(bytearray("<html>\n<body><h1>Your request</h1><p>Your client sent this request</p><pre>" + ans +"</pre></body></html>", "ascii"))
sock.close()

为什么我收到此错误?一直在寻找,但无法真正找到答案..有什么建议吗?

1 个答案:

答案 0 :(得分:3)

问题在于:

sock.sendall(bytearray("HTTP/1.1 200 ok\n", "ascii"))

应该是conn,而不是sock,以及以下几行。

相关问题