Re: [Tutor] how to control putty window
Hello, I changed my way. This time I'm using plink under Putty. My python code is: os.chdir(c:\\Program Files\\Putty) cmd = plink -ssh -l ufuk10.10.10.10 -pw password process = subprocess.Popen(cmd) inputdata=r van result = process.communicate(inputdata) But after the successful SSH, I cannot continue, no command runs: This is the new output after the initial SSH connection: No data input. Last login: Fri Dec 21 16:27: ufuk@home-ubuntu:~$ 2012/12/20 Prasad, Ramit ramit.pra...@jpmorgan.com Ufuk Eskici wrote: Hello, I run this command and opens putty: import os import subprocess command = 'c:\Program Files\Putty\putty.exe -ssh ufukeskici@10.10.10.10 -pw test subprocess.Popen(command) But then I want to input new commands to this Putty new window. How can I do it? Do you need to control Putty or just SSH to another computer? If all you need to SSH then I would recommend using a 3rd party module such as Fabric (which relies on Paramiko). Those modules will simply SSH significantly. They are Python 2.x but you should be able to use Paramiko in 3.x except for SFTP. This link might help to install Paramiko if you are using Python 3. https://github.com/paramiko/paramiko/issues/16 If you need to control Putty specifically then I cannot help. Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to control putty window
Ufuk Eskici wrote: Hello, I changed my way. This time I'm using plink under Putty. My python code is: os.chdir(c:\\Program Files\\Putty) cmd = plink -ssh -l ufuk10.10.10.10 -pw password process = subprocess.Popen(cmd) inputdata=r van result = process.communicate(inputdata) But after the successful SSH, I cannot continue, no command runs: This is the new output after the initial SSH connection: No data input. Last login: Fri Dec 21 16:27: ufuk@home-ubuntu:~$ I am not familiar with plink, so I cannot help you. I recommend using an SSH module which help a lot with all of this. That being said, maybe this post will help: http://code.activestate.com/lists/python-tutor/74807/ Also take a look at the subprocess.communicate documentation[1] as it says Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. **Wait for process to terminate**. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child. communicate() returns a tuple (stdoutdata, stderrdata). Note that if you want to send data to the process's stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too. (emphasis added) This suggests communicate is waiting for the plink to end? Also, you should probably pass in a pipe so that you can send data more than once. Note, I am not familiar with subprocess so YMMV. [1]http://docs.python.org/2.7/library/subprocess.html#subprocess.Popen.communicate Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to control putty window
I used this code: os.chdir(c:\\Program Files\\Putty) cmd = plink -ssh -l ufuk 10.10.10.10 -pw password process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate () After running this code, just one black cmd screen appears, but it frezzes and doesn't continue. I close the window manually and when I write - *print (stdout)* - I can get some output *..ufuk@home-ubuntu:~$ * - as a long string. But I dont know why it freezes and why I cannot input anything. How should I continue? 2012/12/21 Prasad, Ramit ramit.pra...@jpmorgan.com Ufuk Eskici wrote: Hello, I changed my way. This time I'm using plink under Putty. My python code is: os.chdir(c:\\Program Files\\Putty) cmd = plink -ssh -l ufuk10.10.10.10 -pw password process = subprocess.Popen(cmd) inputdata=r van result = process.communicate(inputdata) But after the successful SSH, I cannot continue, no command runs: This is the new output after the initial SSH connection: No data input. Last login: Fri Dec 21 16:27: ufuk@home-ubuntu:~$ I am not familiar with plink, so I cannot help you. I recommend using an SSH module which help a lot with all of this. That being said, maybe this post will help: http://code.activestate.com/lists/python-tutor/74807/ Also take a look at the subprocess.communicate documentation[1] as it says Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. **Wait for process to terminate**. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child. communicate() returns a tuple (stdoutdata, stderrdata). Note that if you want to send data to the process's stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too. (emphasis added) This suggests communicate is waiting for the plink to end? Also, you should probably pass in a pipe so that you can send data more than once. Note, I am not familiar with subprocess so YMMV. [1] http://docs.python.org/2.7/library/subprocess.html#subprocess.Popen.communicate Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to control putty window
On Fri, Dec 21, 2012 at 9:44 AM, Ufuk Eskici ufukesk...@gmail.com wrote: cmd = plink -ssh -l ufuk10.10.10.10 -pw password process = subprocess.Popen(cmd) inputdata=r van result = process.communicate(inputdata) But after the successful SSH, I cannot continue, no command runs: To use communicate(), you need to set one or more of the standard streams to a file or pipe (e.g. stdout=subprocess.PIPE). That said, if you just have a single command, it's simpler to have the ssh client execute it. Get the result using check_output (it sets up and calls communicate): user = 'ufuk' password = 'password' host = 10.10.10.10 remote_cmd = 'r van' cmd = ['plink', '-ssh', '-l', user, '-pw', password, '%s' % remote_cmd] result = subprocess.check_output(cmd, stdin=subprocess.PIPE) I had to add stdin=subprocess.PIPE when trying this interactively. Otherwise plink leaves the console stdin in an unusable state. This is probably the source of the lockup you're getting. If you need an interactive, stateful session, then communicate() won't help since it closes the streams. You'll have to roll your own by manually handling the stdin/stdout pipes. That means you'll need a background thread to get around readline blocking (select only works for sockets on Windows). You'll hit a brick wall with this approach if the program uses full buffering in a pipe. With Linux you can sometimes tweak the streams using stdbuf, but not if the program uses setvbuf(). To get around this in Unix you can use pexpect to fake a tty. I think that's only available on Windows via Cygwin. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Your thoughts on using Python 2.4.x?
On 19/12/12 14:54, boB Stepp wrote: Now after the upgrades some machines now have Python 2.4.4 and others Python 2.4.6. For the purposes of creating/manipulating text files and running Solaris-flavored Unix commands, is there anything I should be specially aware of? I have been working entirely in my Python studies with version 3.x up to this point. I understand that there are differences between 2.x versus 3.x such as print statements versus print functions, etc. Yes; using Python 2.4 is painful compared to Python 3.x because it is missing so many cool and useful features. 2.4 is quite old now, and there have been many, many bug-fixes and new features added since then. Some of those missing features can be easily added into your own code. Because I have to support 2.4, I have a compatibility module to backport the more essential features. http://code.google.com/p/my-startup-file/source/browse/backports.py BTW, does 2.4.x come with Tkinter standard? Depends on what you mean by standard. Tkinter is part of the standard library, but it requires external dependencies which may not be present. If your Solaris system doesn't include tcl (tickle) then you won't have Tkinter either. At the Solaris prompt, enter tclsh. You should get a % prompt, which you can quit by entering exit command. If you don't, then you do not have tcl installed and won't have Tkinter either. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Limitation of int() in converting strings
On 18/12/12 01:36, Oscar Benjamin wrote: I think it's unfortunate that Python's int() function combines two distinct behaviours in this way. In different situations int() is used to: 1) Coerce an object of some type other than int into an int without changing the value of the integer that the object represents. The second half of the sentence (starting from without changing) is not justified. You can't safely make that assumption. All you know is that calling int() on an object is intended to convert the object to an int, in whatever way is suitable for that object. In some cases, that will be numerically exact (e.g. int(1234) will give 1234), in other cases it will not be. 2) Round an object with a non-integer value to an integer value. int() does not perform rounding (except in the most generic sense that any conversion from real-valued number to integer is rounding). That is what the round() function does. int() performs truncating: it returns the integer part of a numeric value, ignoring any fraction part: py from decimal import Decimal as D py from fractions import Fraction as F py int(D(-123.)) -123 py int(F(999, 100)) 9 So you shouldn't think of int(number) as convert number to an int, since that is ambiguous. There are at least six common ways to convert arbitrary numbers to ints: * truncate, or round towards zero (drop any fraction part); * floor, or round towards -infinity (always round down); * ceiling, or round towards +infinity (always round up); * round to nearest, with ties rounding up; * round to nearest, with ties rounding down; * banker's rounding (round to nearest, with ties rounding to the nearest even number) Python provides truncation via the int and math.trunc functions, floor and ceiling via math.floor and math.ceil, and round to nearest via round. In Python 2, ties are rounded up, which is biased; in Python 3, the unbiased banker's rounding is used. Instead, you should consider int(number) to be one of a pair of functions, return integer part, return fraction part, where unfortunately the second function isn't provided directly. In general though, you can get the fractional part of a number with x % 1. For floats, math.modf also works. So, in a sense int() does to double-duty as both a constructor of ints from non-numbers such as strings, and as a get integer part function for numbers. I'm okay with that. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Your thoughts on using Python 2.4.x?
On Fri, Dec 21, 2012 at 5:57 PM, Steven D'Aprano st...@pearwood.info wrote: Yes; using Python 2.4 is painful compared to Python 3.x because it is missing so many cool and useful features. 2.4 is quite old now, and there have been many, many bug-fixes and new features added since then. Some of those missing features can be easily added into your own code. Because I have to support 2.4, I have a compatibility module to backport the more essential features. http://code.google.com/p/my-startup-file/source/browse/backports.py Thanks! I will look into your code in more depth once I get deeper than I currently am in Python. More importantly you've given me a useful approach that I can use for many things (potentially). This type of approach had not yet occurred to me. BTW, does 2.4.x come with Tkinter standard? Depends on what you mean by standard. Tkinter is part of the standard library, but it requires external dependencies which may not be present. If your Solaris system doesn't include tcl (tickle) then you won't have Tkinter either. At the Solaris prompt, enter tclsh. You should get a % prompt, which you can quit by entering exit command. If you don't, then you do not have tcl installed and won't have Tkinter either. Thanks for pointing this out and how to check for tcl being installed. I will have to check for this after the holidays. boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Limitation of int() in converting strings
Oh, another comment... On 18/12/12 01:36, Oscar Benjamin wrote: I have often found myself writing awkward functions to prevent a rounding error from occurring when coercing an object with int(). Here's one: def make_int(obj): '''Coerce str, float and int to int without rounding error Accepts strings like '4.0' but not '4.1' ''' fnum = float('%s' % obj) inum = int(fnum) assert inum == fnum return inum Well, that function is dangerously wrong. In no particular order, I can find four bugs and one design flaw. 1) It completely fails to work as advertised when Python runs with optimizations on: [steve@ando python]$ cat make_int.py def make_int(obj): '''Coerce str, float and int to int without rounding error Accepts strings like '4.0' but not '4.1' ''' fnum = float('%s' % obj) inum = int(fnum) assert inum == fnum return inum print make_int('4.0') print make_int('4.1') # this should raise an exception [steve@ando python]$ python -O make_int.py 4 4 2) Even when it does work, it is misleading and harmful to raise AssertionError. The problem is with the argument's *value*, hence *ValueError* is the appropriate exception, not ImportError or TypeError or KeyError ... or AssertionError. Don't use assert as a lazy way to get error checking for free. 3) Worse, it falls over when given a sufficiently large int value: py make_int(10**500) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 6, in make_int OverflowError: cannot convert float infinity to integer but at least you get an exception to warn you that something has gone wrong. 4) Disturbingly, the function silently does the wrong thing even for exact integer arguments: py n = 10**220 # an exact integer value py make_int(n) == n False 5) It loses precision for string values: py s = 1*200 py make_int(s) % 10 8L And not by a little bit: py make_int(s) - int(s) # should be zero 13582401819835255060712844221836126458722074364073358155901190901 52694241435026881979252811708675741954774190693711429563791133046 96544199238575935688832088595759108887701431234301497L Lest you think that it is only humongous numbers where this is a problem, it is not. A mere seventeen digits is enough: py s = 10001 py make_int(s) - int(s) -1L And at that point I stopped looking for faults. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor