Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSError: [WinError87] The parameter is incorrect

Tags:

python

I have a python script written using python 3.4.3 and it pulls in a .csv file of ip addresses, usernames, and passwords to pass to another batch script.

import pdb
import csv
import os
import subprocess
import datetime
import time
import signal
from multiprocessing import Process

def callGetDimmBatchFile(logFile, batchFileName, ipAddress, userName, passWord):
    print('\nId: {0}'.format(counter) + '\n', file=logFile, flush=True)
    command ='{0} -i {1} -u {2} -p {3}'.format(batchFileName, ipAddress, userName, passWord)
    print(command, file=logFile, flush=True)
    print("IP Address is {0}".format(ipAddress))
    print("User name is {0}".format(userName))
    print("Password is {0}".format(passWord))
    timeout = 60
    start = datetime.datetime.now()
    process = subprocess.Popen(command, stdout=logFile, stderr=logFile)
    while process.poll() is None:
        time.sleep(0.1)
        now = datetime.datetime.now()
        if (now - start).seconds > timeout:
            process.kill()
            # os.kill(process.pid, signal.SIGKILL)
            # os.waitpid(-1, os.Warning)
            return None
    rc = process.wait()
    print('\nReturn Code:', rc, file=logFile, flush=True)


logFile = open('log.txt', 'w+')
batchFileName = 'getfoo.bat'
pathToCsv = 'autorun-input.csv'
print('Path to CSV is {0}'.format(pathToCsv))
counter = 0
with open(pathToCsv) as csvFile:
    reader = csv.reader(csvFile, delimiter=',')
    for row in reader:
        ipAddress = row[0]
        userName = row[1]
        passWord = row[2]
        p = Process(target=callGetDimmBatchFile, args=(logFile, batchFileName, ipAddress, userName, passWord))
        p.start()
        p.join()


        #callGetDimmBatchFile(logFile, batchFileName, ipAddress, userName, passWord)

os.system("pause")

The file (autorun-input.csv) that it reads in is this:

10.69.69.1,taclab,taclab
10.69.69.2,taclab,taclab
10.69.69.3,taclab,taclab
10.69.69.4,taclab,taclab
10.69.69.5,taclab,taclab
10.69.69.6,taclab,taclab
10.69.69.7,taclab,taclab
10.69.69.8,taclab,taclab
10.69.69.9,taclab,taclab
10.69.69.10,taclab,taclab

It is not working on several Windows 7 machines, the error is this:

C:\Users\Taclab\Desktop\DimmScript\Python-Project-9\Python-Project\DimmReport>python autorun.py
Path to CSV is autorun-input.csv
Traceback (most recent call last):
  File "autorun.py", line 44, in <module>
    p.start()
  File "C:\Python34\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Python34\lib\multiprocessing\context.py", line 212, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Python34\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:\Python34\lib\multiprocessing\popen_spawn_win32.py", line 66, in __ini
t__
    reduction.dump(process_obj, to_child)
  File "C:\Python34\lib\multiprocessing\reduction.py", line 59, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: cannot serialize '_io.TextIOWrapper' object

C:\Users\Taclab\Desktop\DimmScript\Python-Project-9\Python-Project\DimmReport>Tr
aceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python34\lib\multiprocessing\spawn.py", line 100, in spawn_main
    new_handle = steal_handle(parent_pid, pipe_handle)
  File "C:\Python34\lib\multiprocessing\reduction.py", line 81, in steal_handle
    _winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] The parameter is incorrect

I don't understand which parameter is incorrect. It appears the exception is thrown at p.open.

like image 266
Jonathan Kittell Avatar asked Sep 27 '22 20:09

Jonathan Kittell


1 Answers

this type of error is usually caused by passing subprocess.Popen a command without an executable. for example:

subprocess.Popen(' -s') # notice the space at the beginning
subprocess.Popen(['','-s']) # this will cause the same error as well

check your log (you wrote the variable 'command' to your log before the error) to see if it is invalid for some reason

if it is ok then i guess it must be the logfile, as the parent process opened it. infact if i try to do the same thing on my pc (i tried it on python2.7) it raises a diffrent error, about the log file being already close.

try doing something like:

with open('tempLog{0}.log'.format(os.getpid(),'w+') as f:
    subprocess.Popen(command, stdout=f, stderr=f)

and see if this works

like image 68
DorElias Avatar answered Oct 04 '22 02:10

DorElias