3

I am new to Socket Programming in Python. I have written the following code in Python 3.7:

trialSocketList.py

import subprocess
import sys

HOST = sys.argv[1]
PORT = sys.argv[2]

command = "tnc " + HOST + " -PORT "
print(command)
subprocess.call(command + PORT)

I am passing the following in the Windows CMD:

python trialSocketList.py "127.0.0.1" 445

But I am having the following error while executing the above code:

tnc 127.0.0.1 -PORT
Traceback (most recent call last):
  File "trialSocketList.py", line 14, in <module>
    subprocess.call(command + PORT)
  File "C:\Python37\lib\subprocess.py", line 323, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Python37\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Python37\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

When I try netstat -an instead of the command tnc 127.0.0.1 -PORT in the same code, the code functions perfectly. I have written the above few lines of code after reading this API.

*I can run tnc command if I hit it directly in Windows cmd.

Am I missing something here? Or is there any other better way of doing this? If so, then please help me understand the issue here.

Thanks in advance.

Code_Ninja
  • 1,729
  • 1
  • 14
  • 38

3 Answers3

1

Try calling Popen with shell=True. Here is how your code would look with that:

import subprocess
import sys

HOST = sys.argv[1]
PORT = sys.argv[2]

command = "tnc " + HOST + " -PORT "
print(command)
process = subprocess.Popen(command, stdout=tempFile, shell=True)

Here is the listed issue.

xilpex
  • 3,097
  • 2
  • 14
  • 45
  • What is tempFile here? I am new to Python, so its difficult to understand that link. – Code_Ninja Apr 19 '19 at 03:12
  • Although I tried to define `tempFile` as a file with `w+` mode. But that left the file empty and the error in the cmd remained the same. The file created was also empty – Code_Ninja Apr 19 '19 at 03:14
  • @Code_Ninja -- All the temp file does is, put the output of the command in it. – xilpex Apr 19 '19 at 03:36
  • okay, I tried to open the file as `tempFile = open("tempFile.txt", "w+")`, this created the file nicely, but the situation with the running of command remained the same. – Code_Ninja Apr 19 '19 at 03:44
1

tnc is a PowerShell command. You'll need to explicitly run it with PowerShell like this:

import subprocess
import sys

HOST = "127.0.0.1"
PORT = 445
command = "tnc " + HOST + " -PORT " + str(PORT)
print(command)
subprocess.call(["powershell.exe",command],stdout=sys.stdout)

Output:

tnc 127.0.0.1 -PORT 445

ComputerName     : 127.0.0.1
RemoteAddress    : 127.0.0.1
RemotePort       : 445
InterfaceAlias   : Loopback Pseudo-Interface 1
SourceAddress    : 127.0.0.1
TcpTestSucceeded : True
glhr
  • 4,439
  • 1
  • 15
  • 26
-1

The issue here is that the python script can't find the tnc program. Either the program is not installed at all, or---if it is installed---it isn't in the PATH variable.

Tyler Marshall
  • 489
  • 3
  • 9
  • But, I can run the same from the cmd window or the powershell. – Code_Ninja Apr 11 '19 at 00:50
  • You never said you could run it at all. At least that means `tnc` is installed. The fact that you can run it from the command line says that there is something going on when it tries to find the program in your path variable. You can try specifying the full path as part of the subprocess call or you can try setting `shell=True`. Also, how is it in your path? There is also this: https://bugs.python.org/issue17023 – Tyler Marshall Apr 11 '19 at 15:51
  • The path is in not in quotes, I will try `shell=True`. – Code_Ninja Apr 12 '19 at 04:47
  • But since I am using windows, will `shell=True` be applicable? I thought its for Unix/Linux systems only. – Code_Ninja Apr 12 '19 at 04:48
  • It is still applicable. It will use something like `cmd` to execute instead of just creating a new process like the `subprocess.call()` w/o `shell=True`. – Tyler Marshall Apr 15 '19 at 15:12
  • `shell=True` didn't help. – Code_Ninja Apr 19 '19 at 03:04