Python在recv上的Node中导致ECONNRESET错误

时间:2015-02-18 22:23:57

标签: python node.js

我正在开发一个需要解析JavaScript文件的程序。我正在使用Node.js来托管一个名为Esprima的JavaScript解析器。我在localhost上使用TCP连接来连接节点。我将Node设置为服务器,将Python设置为客户端。 Python脚本将js文件发送到Node,Node将解析树发送回Python。当Python调用sock.recv()以从Node获取响应时,Node会收到ECONNRESET错误。 Python就在这里

# Scancode.py

# Import libraries
import json
import sys
import glob
import errno
import socket
import os
import time
from xml.etree.ElementTree import Element, SubElement, Comment, tostring

#Address that we will pass to the socket that we will use to talk to node
TCP_IP = 'localhost'

#The port number that we use to talk to node. It is nice and high so we don't accidentally use a common port.
TCP_PORT = 8080

#Size for the TCP buffer
BUFFER_SIZE = 20000

#acceptable number of command line arguments 
num_args = 4

#total number of possible command line options
num_optns = 1;

#error message to indicate proper usage
usage = "Error: Scancode.py [-recursive] <js-source> <xml-output>"

#get the command line arguments
args = sys.argv

#test number of arguments
if len(args) > num_args and len(args) < num_args - num_optns:
    print usage
    sys.exit(0)

#remember the name of the js path; should be the second argument
path = args[len(args) - 2]

dirs = os.listdir(path)
os.chdir(path)

myjs = []
for file in dirs:
try:
#file element in the XML
file1 = Element('file')
fname = os.path.basename(file)
    with open(file) as f:
        myjs.append(f.read())
except IOError as exc:
    if exc.errno != errno.EISDIR:
        raise

#Create a new TCP socket using IPv4 protocol
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#Attempt to connect to Esprima
sock.connect((TCP_IP, TCP_PORT))

#send js file over
sock.send(myjs[0])

#receive host responses
response = sock.recv(BUFFER_SIZE)

#print results in proper JSON format
print response

#close the connection
sock.send("end")
sock.close()

和Node在这里

var net = require('net');
var esprima = require('esprima');
var server = net.createServer(function(c) { //'connection' listener
  console.log('client connected');
  c.on('end', function() {
    console.log('client disconnected');
  });
  c.on('data', function(data) {
    if(data.toString() == "end") {
      c.end();
    } else {
      var response = JSON.stringify(esprima.parse(data, {loc: true}), null, 4);
      c.write(response)
    }   
  });

  c.on('error', function(ex) {
    console.log("handled error");
    console.log(ex);
  });
});
server.listen(8080, function() { //'listening' listener
  console.log('server bound');
});

当我使用只有几行的文件进行测试时,一切正常。但是当我发送一个更大的文件时,Node会收到一个ECONNRESET错误。现在我在Node中有一个错误处理程序,但我不知道错误发生的原因。

我通过命令node host启动节点服务器,在命令提示符下运行Windows机器。  在另一个命令提示符中,我使用命令Scancode.py -recursive input anything运行Python。

通过执行此操作,Python脚本需要在当前工作目录中调用名为input的文件目录。我的输入目录包含四个js文件。以下是两个文件。第一个导致错误,第二个导致错误。

File1中:

function setUser(sName, uid, name) {
    if (0 < document.getElementsByName("UID_"+sName).length) {
        popField("UID_" + sName, uid);
    }

    if (0 < document.getElementsByName("NAME_"+sName).length) {
        popField("NAME_" + sName, name);
    }

    if (0 < document.getElementsByName(sName).length) {
        popField(sName, uid);
    }

    hideElement("getUserFrame");
}

function popField(fieldName, val) {
    if (document.getElementsByName(fieldName)[0].tagName.toUpperCase() == "INPUT")
        document.getElementsByName(fieldName)[0].value = val;
    else
        document.getElementsByName(fieldName)[0].innerHTML = val;
}
function getUser(sName) {
    document.getElementById("getUserFrame").src = "/iTrust/util/getUser.jsp?s=" + sName;
    showElement("getUserFrame");
    centerElement("getUserFrame");
}
function getUserClose() {
    hideElement("getUserFrame");
}

文件2:

var answer = 42

如果要测试它,只需在Python脚本所在的目录中创建一个目录。并将这些文件放入其中。

在第64行:sock.send(myjs[0]),将索引从0更改为1将发送第二个文件而不是目录中的第一个文件。

我已经安装了Node模块esprima和net。

0 个答案:

没有答案