python套接字-无法从javascript获取发送的数据
我试图让javascript程序向python套接字发送数据,但它没有收到正确的数据 我想让python打印“aaaa” 以下是我的javascript代码:python套接字-无法从javascript获取发送的数据,javascript,python,sockets,python-3.x,Javascript,Python,Sockets,Python 3.x,我试图让javascript程序向python套接字发送数据,但它没有收到正确的数据 我想让python打印“aaaa” 以下是我的javascript代码: function createCORSRequest(method, url) { var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr) { xhr.open(method, url, true); } else if (typeof XDoma
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('GET', "http://192.168.1.10:12345");
xhr.send("aaaa");
以下是我的python代码:
import socket
s = socket.socket()
host = socket.gethostname()
port = 12345
BUFFER_SIZE = 1024
s.bind(('', port))
s.listen(5)
while True:
c, addr = s.accept()
print ('Got connection from', addr)
c.send(bytes('Thank you for connecting','UTF-8'))
data = c.recv(BUFFER_SIZE)
print(data)
c.close()
xhr.send(“aaaa”)
)将被忽略(表示:不发送)。要发送HTTP正文,请使用POST等请求类型xhr.send(“aaaa”)
)将被忽略(表示:不发送)。要发送HTTP正文,请使用POST等请求类型Steffen的回答是正确的(至少在一般情况下——不能对JS的细节发表评论)。此外,独立验证应用程序的活动部分始终是一个好主意,这样可以缩小问题的范围 以下是如何从命令行验证python服务器是否正常工作:
telnet本地主机12345
(它将首先尝试使用IPv6连接,失败后返回IPv4)Enter
meow
发送到服务器:
margold@home-macbook ~ $ telnet 127.0.0.1 12345
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Thank you for connectingmeow
Connection closed by foreign host.
margold@home-macbook ~ $ ./server.py
('Got connection from', ('127.0.0.1', 61148))
meow
对于服务器:
margold@home-macbook ~ $ telnet 127.0.0.1 12345
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Thank you for connectingmeow
Connection closed by foreign host.
margold@home-macbook ~ $ ./server.py
('Got connection from', ('127.0.0.1', 61148))
meow
Steffen的回答是正确的(至少在一般情况下——不能对JS的细节发表评论)。此外,独立验证应用程序的活动部分始终是一个好主意,这样可以缩小问题的范围 以下是如何从命令行验证python服务器是否正常工作:
telnet本地主机12345
(它将首先尝试使用IPv6连接,失败后返回IPv4)Enter
meow
发送到服务器:
margold@home-macbook ~ $ telnet 127.0.0.1 12345
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Thank you for connectingmeow
Connection closed by foreign host.
margold@home-macbook ~ $ ./server.py
('Got connection from', ('127.0.0.1', 61148))
meow
对于服务器:
margold@home-macbook ~ $ telnet 127.0.0.1 12345
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Thank you for connectingmeow
Connection closed by foreign host.
margold@home-macbook ~ $ ./server.py
('Got connection from', ('127.0.0.1', 61148))
meow
您是否通过浏览器发送javascript?是的,我通过浏览器发送javascript。您是否通过浏览器发送javascript?是的,我通过浏览器发送javascript