简体   繁体   中英

Starting gnome-terminal with tuple list arguments

There have been numerous questions about this topic, but I reassure you, non of them seemed to work for me. From Python script A.py, I want to open another python script, B.py, in new window, passing through some arguments:

ListA = [('192.168.2.1', '192.168.2.8'), ('192.168.2.3', '192.168.2.5'), ('192.168.2.8', '192.168.2.4')]
Interface = "wlan0"

Normally, I would pass them through via the commend-line: B.py -i Interface -p ListA

But at the moment I am having some trouble with passing them through, this is what I've tried:

import subprocess, shlex, os, sys
subprocess.Popen(shlex.split('gnome-terminal -x bash "' + os.path.join(os.path.dirname(__file__), 'B.py') + ' -i ' + Interface + ' -p "' + str(ListA) + '""'), stdout=subprocess.PIPE)

and:

subprocess.Popen(['gnome-terminal', '-x', '"bash', '-c', os.path.join(os.path.dirname(__file__), 'B.py'), '-i', Interface, '-p', '"' + str(ListenPipes) + '""'], stdout=subprocess.PIPE)

I got multiple errors throwing 'invalid syntax', 'End Of Line while scanning string literal'

B.py:

if __name__ == "__main__":
    if len(sys.argv) == 1:
        print 'Specify all arguments: B.py -i <Interface> -p <Pipe tuple list [(StartPipe, EndPipe), ..]>'
        sys.exit()
    interface = 'wlan0'
    try:
        opts, args = getopt.getopt(sys.argv[1:],"hi:p:",["interface","pipes"])
    except getopt.GetoptError:
        print 'B.py -i <Interface> -p <Pipe tuple list [(StartPipe, EndPipe), ..]>'
        sys.exit()
    for opt, arg in opts:
        if opt == '-h':
            print 'usage: B.py -i <Interface> -p <Pipe tuple list [(StartPipe, EndPipe), ..]>'
            sys.exit()
        elif opt in ("-i", "--interface"):
            interface = re.compile('\033\[\d+(?:;\d+)?m').sub('',arg) #Remove bash colors
        elif opt in ("-p", "--pipes"):
            ListenPipes = ast.literal_eval(arg)

I just cant get my script to work, any ideas will be appreciated

My first (maybe silly) idea was to create a tempfile from A.py, write there a Bash script to run B.py with all its arguments, add +X flag and spawn gnome-terminal with tempfile as an argument to run. Is this sollution good enough for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM