为什么Node.js HTTP服务器不响应来自Python的请求?

我有一个工作的HTTP node.js服务器。

然后我在python上创build了一个程序,它使用套接字模块连接到上面的服务器

请暂时不要介意try和except语句。 代码的connectTO()函数就像任何其他代码一样简单地连接到一个服务器,除了它处理一些错误。 然后程序发送消息"hello" 。 接下来在while循环中,它重复地等待一个答案,当它接收到一个答案时,它将打印答案。

当我从python连接到Node.js http服务器时,我得到的消息是:

 "You have just succesfully connected to the node.js server" 

如果你看我的代码意味着s.connect(())命令是成功的。 我的问题是,当一个请求发送到服务器,它应该输出一个消息,但它没有。

我也尝试向服务器发送消息,在这种情况下,服务器发回以下消息:

HTTP / 1.1 400错误的请求

那为什么服务器没有响应请求? 为什么拒绝他们?

Python客户端:

 from socket import AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR import threading, socket, time, sys s = socket.socket(AF_INET,SOCK_STREAM) def connectTO(host,port): connect = False count = 0 totalCount = 0 while connect!= True: try: s.connect((host,port)) connect = True print("You have just succesfully connected to the node.js server") except OSError: count += 1 totalCount += 1 if totalCount == 40 and count == 4: print("Error: 404. Connection failed repeatedly") sys.exit(0) elif count == 4: print("Connection failed, retrying...") count = 0 else: pass connectTO("IP_OF_NODE.jS_SERVER_GOES_HERE",777) message = "hello" s.send(message.encode("utf-8")) while True: try: data, addr = s.recvfrom(1024) if data == "": pass else: print(data.decode()) except ConnectionResetError: print("it seems like we can't reach the server anymore..") print("This could be due to a change in your internet connection or the server.") s.close() 

Node.js HTTP服务器:

 function onRequest(req, res) { var postData = ""; var pathname = url.parse(req.url).pathname; //Inform console of event recievent console.log("Request for "+pathanme+" received."); //Set the encoding to equivelant one used in html req.setEncoding("utf8"); //add a listener for whenever info comes in and output full result req.addListener("data", function(postDataChunk) { postData += postDataChunk; console.log("Received POST data chunk: '"+postDataChunk+"'"); }); req.addListener("end", function() { route(handle, pathname, res, frontPage, postData); }); }; http.createServer(onRequest).listen(port,ip); console.log("Server has started."); 

我的一些研究

我也应该注意到,经过一番研究,似乎HTTP服务器接受HTTP请求,但我不明白维基百科上的大部分内容。 这是服务器没有响应的原因吗? 如何解决这个问题,同时仍然使用socket模块。

还有堆栈溢出很多类似的问题,但没有帮助我解决我的问题。 其中之一描述了我的问题,唯一的答案是关于“握手”。 谷歌在这里也是毫无意义的,但从我的理解来看,它只是服务器和客户端之间的一个反应,它定义了协议的内容。 这可能是我错过了什么,我该如何执行它?

其中一些问题也使用了模块,我还没有准备好使用像websocket 。 无论是它们还是它们描述了服务器连接到客户端的方式,可以通过直接调用python代码或通过Node.js express连接到它。 我希望客户端是通过python中的套接字模块连接到HTTP服务器的客户端 。 为了未来的访问者寻找这样的事情,这里有一些问题:

  • 如何连接node.js应用程序与Python脚本?
  • Python客户端连接到使用Socket.IO的nodeJS服务器
  • Python连接到HTTP服务器
  • 一个博客也做类似于上面描述的东西: https : //www.sohamkamani.com/blog/2015/08/21/python-nodejs-comm/

这里的答案实际上并不明显,但也解决了这个问题只有相关的代码。 一般来说,服务器并不多的人可能会错过: 如何使用套接字获取网页使用python

您将需要构build一个HTTP请求。

例如: GET / HTTP/1.1\n\n

尝试这个:

 from socket import AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR import threading, socket, time, sys s = socket.socket(AF_INET,SOCK_STREAM) def connectTO(host,port): connect = False count = 0 totalCount = 0 while connect!= True: try: s.connect((host,port)) connect = True print("You have just succesfully connected to the node.js server") except OSError: count += 1 totalCount += 1 if totalCount == 40 and count == 4: print("Error: 404. Connection failed repeatedly") sys.exit(0) elif count == 4: print("Connection failed, retrying...") count = 0 else: pass connectTO("IP_OF_NODE.jS_SERVER_GOES_HERE",777) message = "GET / HTTP/1.1\n\n" s.send(message.encode("utf-8")) while True: try: data, addr = s.recvfrom(1024) if data == "": pass else: print(data.decode()) except ConnectionResetError: print("it seems like we can't reach the server anymore..") print("This could be due to a change in your internet connection or the server.") s.close() 

阅读这个了解更多关于HTTP。

现在,我build议使用这个python库来做你正在做的事情。 它使事情变得更容易。 但是,如果您使用原始套接字100%设置,那么您应该使节点服务器也使用原始套接字。 (假设你只能通过python连接)。 这是一个很好的教程