Re: kmeans clustering
Sharan Basappa writes: > Can someone please help me to clarify the different between fit and > predict functions of kmeans? > What is the significance of `predict' in K-means? It is an unsupervised clustering algorithm. My intuition says that the cluster composition itself might change if you add a new example and re-run K-means. On the other hand if you don't want to change the cluster compositions and just want to find a cluster for a new example then it is not K-means. In that case it is Knearest classifier. -- Pankaj Jangid -- https://mail.python.org/mailman/listinfo/python-list
Re: numpy results in segmentation fault
Yes it is crashing in the hackerrank site and the testcases fails with segmentation fault. I tried to install numpy with 3.7.3 and it is for some reason not working and after import when I run import numpy at python console and press enter I get >>? i,e its not working properly. Can you please help letting me know the python and numpy compatibility matrix or I am missing anything? I tried some of the numpy code from the other github and it also fails with the segmentation fault :-(. I am guessing some numpy version compatility issue or some environment issue. On Thu, Sep 12, 2019 at 8:00 PM Thomas Jollans wrote: > On 12/09/2019 15.53, Pradeep Patra wrote: > > Hi , > > > > I was trying to solve the hackerrank and was using python 3.7.x. > > https://www.hackerrank.com/challenges/np-concatenate/problem > > > > While running the code sometimes I get success result and sometimes it > > fails with "Segmentation Fault" at Hacker rank UI. I dont have any clue > why > > the code is crashing ? Does anyone have any idea? > > > Are you sure it's your code that's crashing, and not something beyond > your control? (Such as the software that is starting Python for you) > Does it depend on the input? Can you reproduce the issue in a controlled > environment (i.e. on your own PC)? > > > > > > Regards > > Pradeep > > > > import numpy > > > > n,m,p = map(int,input().split()) > > tgt_arr1 = [] > > for i in range(n): > > row = list(map(int,input().split())) > > tgt_arr1.append(row) > > tgt_arr2 = [] > > for j in range(m): > > row = list(map(int,input().split())) > > tgt_arr2.append(row) > > > > num_arr1 = numpy.array(tgt_arr1,int) > > num_arr2 = numpy.array(tgt_arr2,int) > > > > print(numpy.concatenate((num_arr1,num_arr2),axis=0)) > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
OT: Using a fake Gmail address is probably not a good idea
(I would have sent this off-list, but for obvious reasons I couldn't.) Mails for someone here who goes by the handle "ast" with a fake address of n...@gmail.com keep landing in my Gmail spam folder. I suspect the same is true for all people subscribed to python-list who use Gmail. Gmail (correctly, I think) can't verify that the mail I received actually originated there. It appears this user is actually posting via Usenet through the server news.free.fr (apparently ProxAd). I check my spam folder regularly for false positives, but I'm sure many people don't. Whoever you are, ast, you might want to reconsider your choice of fake return address. Skip -- https://mail.python.org/mailman/listinfo/python-list
Fwd: numpy results in segmentation fault
Please reply on-list. (both of you) Forwarded Message Subject:Re: numpy results in segmentation fault Date: Mon, 16 Sep 2019 17:04:57 +0530 From: Test Bot To: Pradeep Patra CC: Thomas Jollans Firstly, in response to this " I tried to install numpy with 3.7.3 and it is for some reason not working and after import when I run import numpy at python console and press enter I get >>? i,e its not working properly. " the >> prompt after import numpy signifies that the numpy module has been loaded and is available in the session. Can you please provide the traceback you are getting along with the input. PS - Also, you have some coins like thing on hackerrank I guess to reveal the test cases, in case everything else fails. On Mon, Sep 16, 2019 at 3:08 PM Pradeep Patra mailto:smilesonisa...@gmail.com>> wrote: Yes it is crashing in the hackerrank site and the testcases fails with segmentation fault. I tried to install numpy with 3.7.3 and it is for some reason not working and after import when I run import numpy at python console and press enter I get >>? i,e its not working properly. Can you please help letting me know the python and numpy compatibility matrix or I am missing anything? I tried some of the numpy code from the other github and it also fails with the segmentation fault :-(. I am guessing some numpy version compatility issue or some environment issue. On Thu, Sep 12, 2019 at 8:00 PM Thomas Jollans mailto:t...@tjol.eu>> wrote: > On 12/09/2019 15.53, Pradeep Patra wrote: > > Hi , > > > > I was trying to solve the hackerrank and was using python 3.7.x. > > https://www.hackerrank.com/challenges/np-concatenate/problem > > > > While running the code sometimes I get success result and sometimes it > > fails with "Segmentation Fault" at Hacker rank UI. I dont have any clue > why > > the code is crashing ? Does anyone have any idea? > > > Are you sure it's your code that's crashing, and not something beyond > your control? (Such as the software that is starting Python for you) > Does it depend on the input? Can you reproduce the issue in a controlled > environment (i.e. on your own PC)? > > > > > > Regards > > Pradeep > > > > import numpy > > > > n,m,p = map(int,input().split()) > > tgt_arr1 = [] > > for i in range(n): > > row = list(map(int,input().split())) > > tgt_arr1.append(row) > > tgt_arr2 = [] > > for j in range(m): > > row = list(map(int,input().split())) > > tgt_arr2.append(row) > > > > num_arr1 = numpy.array(tgt_arr1,int) > > num_arr2 = numpy.array(tgt_arr2,int) > > > > print(numpy.concatenate((num_arr1,num_arr2),axis=0)) > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
What is the Difference Between quit() and exit() commands in Python?
What is the Difference Between quit() and exit() commands in Python? -- https://mail.python.org/mailman/listinfo/python-list
Re: What is the Difference Between quit() and exit() commands in Python?
Hongyi Zhao wrote: > What is the Difference Between quit() and exit() commands in Python? They are instances of the same type >>> import inspect >>> type(quit) is type(exit) True >>> print(inspect.getsource(type(quit))) class Quitter(object): def __init__(self, name, eof): self.name = name self.eof = eof def __repr__(self): return 'Use %s() or %s to exit' % (self.name, self.eof) def __call__(self, code=None): # Shells like IDLE catch the SystemExit, but listen when their # stdin wrapper is closed. try: sys.stdin.close() except: pass raise SystemExit(code) There is no difference, except for the name attribute and the repr() text: >>> exit.name, exit.eof, exit ('exit', 'Ctrl-D (i.e. EOF)', Use exit() or Ctrl-D (i.e. EOF) to exit) >>> quit.name, quit.eof, quit ('quit', 'Ctrl-D (i.e. EOF)', Use quit() or Ctrl-D (i.e. EOF) to exit) -- https://mail.python.org/mailman/listinfo/python-list
Strange Class definition
Hello Following syntax doesn't generate any errors: >>> foo=0 >>> Class Foo: foo But class Foo seems empty Is it equivalent to ? >>> class Foo: pass -- https://mail.python.org/mailman/listinfo/python-list
Re: What is the Difference Between quit() and exit() commands in Python?
On 9/16/19, Hongyi Zhao wrote: > > What is the Difference Between quit() and exit() commands in Python? They're different instances of the Quitter class, which is available if site.py is imported (i.e. not with the -S command-line option). They're created by site.setquit(): def setquit(): """Define new builtins 'quit' and 'exit'. These are objects which make the interpreter exit when called. The repr of each object contains a hint at how it works. """ if os.sep == '\\': eof = 'Ctrl-Z plus Return' else: eof = 'Ctrl-D (i.e. EOF)' builtins.quit = _sitebuiltins.Quitter('quit', eof) builtins.exit = _sitebuiltins.Quitter('exit', eof) exit(code) or quit(code) closes sys.stdin and raises SystemExit(code): >>> quit.__class__ >>> print(inspect.getsource(type(quit))) class Quitter(object): def __init__(self, name, eof): self.name = name self.eof = eof def __repr__(self): return 'Use %s() or %s to exit' % (self.name, self.eof) def __call__(self, code=None): # Shells like IDLE catch the SystemExit, but listen when their # stdin wrapper is closed. try: sys.stdin.close() except: pass raise SystemExit(code) For example: >>> try: ... quit(42) ... except BaseException as e: ... print(repr(e)) ... SystemExit(42,) >>> sys.stdin.closed True Alternatively, sys.exit is a builtin function that's equivalent to `raise SystemExit(code)` but does not close sys.stdin. For example: >>> try: ... sys.exit(42) ... except BaseException as e: ... print(repr(e)) ... SystemExit(42,) >>> sys.stdin.closed False -- https://mail.python.org/mailman/listinfo/python-list
Re: Strange Class definition
ast wrote: > Hello > > Following syntax doesn't generate any errors: > > >>> foo=0 > >>> Class Foo: > foo > > But class Foo seems empty > > Is it equivalent to ? > > >>> class Foo: > pass The resulting class is equivalent, but the expression `foo` is actually evaluated during class creation. You can easily see this when you replace foo with a print call, say: >>> for foo in "first", "second": ... class A: print(foo) ... first second If you compare the byte code you may note a small difference to def f(): foo >>> import dis >>> c = compile("class A: foo", ">> dis.dis(c) 1 0 LOAD_BUILD_CLASS 1 LOAD_CONST 0 () 4 LOAD_CONST 1 ('A') 7 MAKE_FUNCTION0 10 LOAD_CONST 1 ('A') 13 CALL_FUNCTION2 (2 positional, 0 keyword pair) 16 STORE_NAME 0 (A) 19 LOAD_CONST 2 (None) 22 RETURN_VALUE >>> c.co_consts[0] >>> dis.dis(c.co_consts[0]) 1 0 LOAD_NAME0 (__name__) 3 STORE_NAME 1 (__module__) 6 LOAD_CONST 0 ('A') 9 STORE_NAME 2 (__qualname__) The relevant instruction: 12 LOAD_NAME3 (foo) 15 POP_TOP 16 LOAD_CONST 1 (None) 19 RETURN_VALUE For comparison a function: >>> def f(): foo ... >>> dis.dis(f) 1 0 LOAD_GLOBAL 0 (foo) 3 POP_TOP 4 LOAD_CONST 0 (None) 7 RETURN_VALUE In the class body LOAD_NAME is used instead of LOAD_GLOBAL. This allows dynamical scoping >>> foo = "outer" >>> class A: ... print(foo) ... foo = "inner" ... print(foo) ... outer inner >>> A.foo 'inner' whereas in the function you get an error: >>> def f(): ... print(foo) ... foo = "inner" ... print(foo) ... >>> f() Traceback (most recent call last): File "", line 1, in File "", line 2, in f UnboundLocalError: local variable 'foo' referenced before assignment -- https://mail.python.org/mailman/listinfo/python-list
Re: numpy results in segmentation fault
Firstly, in response to this " I tried to install numpy with 3.7.3 and it is for some reason not working and after import when I run import numpy at python console and press enter I get >>? i,e its not working properly. " the >> prompt after import numpy signifies that the numpy module has been loaded and is available in the session. Can you please provide the traceback you are getting along with the input. PS - Also, you have some coins like thing on hackerrank I guess to reveal the test cases, in case everything else fails. On Mon, Sep 16, 2019 at 5:32 PM Thomas Jollans wrote: > Please reply on-list. (both of you) > > > Forwarded Message > Subject: Re: numpy results in segmentation fault > Date: Mon, 16 Sep 2019 17:04:57 +0530 > From: Test Bot > To: Pradeep Patra > CC: Thomas Jollans > > Firstly, in response to this > " > I tried to install numpy with 3.7.3 and it is for some > reason not working and after import when I run import numpy at python > console and press enter I get >>? i,e its not working properly. > " > > the >> prompt after import numpy signifies that the numpy module has been > loaded and is available in the session. > > Can you please provide the traceback you are getting along with the input. > > PS - Also, you have some coins like thing on hackerrank I guess to reveal > the test cases, in case everything else fails. > > On Mon, Sep 16, 2019 at 3:08 PM Pradeep Patra > wrote: > >> Yes it is crashing in the hackerrank site and the testcases fails with >> segmentation fault. I tried to install numpy with 3.7.3 and it is for some >> reason not working and after import when I run import numpy at python >> console and press enter I get >>? i,e its not working properly. >> >> Can you please help letting me know the python and numpy compatibility >> matrix or I am missing anything? >> >> I tried some of the numpy code from the other github and it also fails >> with >> the segmentation fault :-(. I am guessing some numpy version compatility >> issue or some environment issue. >> >> On Thu, Sep 12, 2019 at 8:00 PM Thomas Jollans wrote: >> >> > On 12/09/2019 15.53, Pradeep Patra wrote: >> > > Hi , >> > > >> > > I was trying to solve the hackerrank and was using python 3.7.x. >> > > https://www.hackerrank.com/challenges/np-concatenate/problem >> > > >> > > While running the code sometimes I get success result and sometimes it >> > > fails with "Segmentation Fault" at Hacker rank UI. I dont have any >> clue >> > why >> > > the code is crashing ? Does anyone have any idea? >> > >> > >> > Are you sure it's your code that's crashing, and not something beyond >> > your control? (Such as the software that is starting Python for you) >> > Does it depend on the input? Can you reproduce the issue in a controlled >> > environment (i.e. on your own PC)? >> > >> > >> > > >> > > Regards >> > > Pradeep >> > > >> > > import numpy >> > > >> > > n,m,p = map(int,input().split()) >> > > tgt_arr1 = [] >> > > for i in range(n): >> > > row = list(map(int,input().split())) >> > > tgt_arr1.append(row) >> > > tgt_arr2 = [] >> > > for j in range(m): >> > > row = list(map(int,input().split())) >> > > tgt_arr2.append(row) >> > > >> > > num_arr1 = numpy.array(tgt_arr1,int) >> > > num_arr2 = numpy.array(tgt_arr2,int) >> > > >> > > print(numpy.concatenate((num_arr1,num_arr2),axis=0)) >> > >> > >> > -- >> > https://mail.python.org/mailman/listinfo/python-list >> > >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > -- https://mail.python.org/mailman/listinfo/python-list
Re: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time
On Mon, Sep 16, 2019 at 4:38 AM Spencer Graves wrote: > >Is anyone interested in contacting these companies -- or the > companies from which they buy cybersecurity insurance -- and inviting > them to provide paid staff to maintain 2.7 and to offer further offer > consulting services to help these clients inventory what they have and > how much it would cost to migrate? > > >For example, how much would it cost to write and maintain an > emulator for 2.7.16 in 3.7.4? > > >The Python Software Foundation does not want to maintain 2.7 for > free anymore, but if there is sufficient demand, they should be thrilled > to make a handsome profit off of it -- while providing high quality, > good paying jobs for smart Pythonistas. > That's not really the PSF's job, but if you're looking at this from a viewpoint of "wouldn't it be nice if there were jobs available supporting 2.7", then do the rounds of the commercial Python distributors. Anaconda, Enthought, ActiveState, and possibly folks like Red Hat, have an interest in making money off Python, and they're not in any way obliged to stop working with Py2 as of 2020. Each company is free to make its own commercial decision regarding which versions they'll support. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
RE: What is the Difference Between quit() and exit() commands in Python?
Hi exit (http://docs.python.org/2/library/constants.html#exit"; rel="noreferrer) is an alias for quit (or vice-versa). They exist together simply to make Python more user-friendly. please refer: https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used (https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used) -ursprüngliche Nachricht- Von: hongyi.z...@gmail.com (mailto:hongyi.z...@gmail.com) Gesendet: 16.09.2019 14:35 Uhr An: python-list@python.org (mailto:python-list@python.org) Betreff: What is the Difference Between quit() and exit() commands in Python? What is the Difference Between quit() and exit() commands in Python? -- mail.python.org/mailman/listinfo/python-list (https://mail.python.org/mailman/listinfo/python-list"; target="_blank" rel="noopener) -ursprüngliche Nachricht Ende- -- https://mail.python.org/mailman/listinfo/python-list
Re: OT: Using a fake Gmail address is probably not a good idea
On Mon, Sep 16, 2019 at 1:56 PM Skip Montanaro wrote: > Mails for someone here who goes by the handle "ast" with a fake > address of n...@gmail.com keep landing in my Gmail spam folder. I > suspect the same is true for all people subscribed to python-list who > use Gmail. Gmail (correctly, I think) can't verify that the mail I > received actually originated there. It is true for any server that is applying DMARC policies. And having to deal with his messages is also very annoying to me. Ast should use a proper invalid email address (E.g. anything ending in .invalid, but there are also other reserved domains for such purposes.) if he does not want to reveal his real address, instead of making up a possibly valid address. -- https://mail.python.org/mailman/listinfo/python-list
Something faster than turtle
Hi, I'm novice in Python. I'm trying to draw with turtle but it's really slow (even with speed("fastest")). Comparing to Scratch, it's really slow. 1/ Are there solutions to get things faster ? 2/ Are there any other tools such as turtle but (really) faster ? Thanks. -- https://mail.python.org/mailman/listinfo/python-list
re not working
For some reason these are different: pattern = r'[0-9]{4,6}' And pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' And when I try to match them import re re.search(pattern, '1234') and import re re.search(pattern2, '1234') are different. Help? -- https://mail.python.org/mailman/listinfo/python-list
Irritating bytearray behavior
I am using a bytearray to construct a very simple message, that will be sent across the network. The message should always be 20 bytes: 2 bytes - address family (AF_INET or AF_INET6) - network byte order 2 bytes - (padding) 4 or 16 bytes - IP address The size of the IP address is dependent on whether it is an IPv4 address (4 bytes) or an IPv6 address (16 bytes). In the IPv4 case, it should be followed by 12 bytes of padding, to keep the message size consistent. Naïvely, I thought that I could do this: ip = ipaddress.ip_address(unicode(addr)) msg = bytearray(20) msg[1] = socket.AF_INET if ip.version == 4 else socket.AF_INET6 msg[4:] = ip.packed sock.sendto(msg, dest) This doesn't work in the IPv4 case, because the bytearray gets truncated to only 8 bytes (4 bytes plus the size of ip.packed). Is there a way to avoid this behavior copy the contents of ip.packed into the bytearray without changing its size? TIA -- Ian Pilcher arequip...@gmail.com "I grew up before Mark Zuckerberg invented friendship" -- https://mail.python.org/mailman/listinfo/python-list
Re: re not working
On 2019-09-17 02:31, CrazyVideoGamez wrote: For some reason these are different: pattern = r'[0-9]{4,6}' And pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' And when I try to match them import re re.search(pattern, '1234') and import re re.search(pattern2, '1234') are different. Help? Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> pattern = r'[0-9]{4,6}' >>> pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' >>> re.search(pattern, '1234') >>> re.search(pattern2, '1234') They look the same to me. -- https://mail.python.org/mailman/listinfo/python-list
Re: Irritating bytearray behavior
On 16Sep2019 20:37, Ian Pilcher wrote: msg[4:] = ip.packed sock.sendto(msg, dest) This doesn't work in the IPv4 case, because the bytearray gets truncated to only 8 bytes (4 bytes plus the size of ip.packed). Is there a way to avoid this behavior copy the contents of ip.packed into the bytearray without changing its size? Sure: msg[4:4+len(ip.packed)] = ip.packed What you did was ask to replace all the bytes from 4 onward. What you should do is replace just the correct bytes. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: Irritating bytearray behavior
On 2019-09-17 02:37, Ian Pilcher wrote: I am using a bytearray to construct a very simple message, that will be sent across the network. The message should always be 20 bytes: 2 bytes - address family (AF_INET or AF_INET6) - network byte order 2 bytes - (padding) 4 or 16 bytes - IP address The size of the IP address is dependent on whether it is an IPv4 address (4 bytes) or an IPv6 address (16 bytes). In the IPv4 case, it should be followed by 12 bytes of padding, to keep the message size consistent. Naïvely, I thought that I could do this: ip = ipaddress.ip_address(unicode(addr)) msg = bytearray(20) msg[1] = socket.AF_INET if ip.version == 4 else socket.AF_INET6 msg[4:] = ip.packed sock.sendto(msg, dest) This doesn't work in the IPv4 case, because the bytearray gets truncated to only 8 bytes (4 bytes plus the size of ip.packed). Is there a way to avoid this behavior copy the contents of ip.packed into the bytearray without changing its size? The truncation you're seeing is perfectly normal. It's the same as for lists. Try this: msg[4 : 4 + len(ip.packed)] = ip.packed Alternatively, you could build the message a bytestrings and then pad with the .ljust method. -- https://mail.python.org/mailman/listinfo/python-list
python3 subprocess run sudo cmd in remote failed
Hello, I use python3.5 and found no way to solve this problem >from subprocess import Popen, PIPE >ps = Popen('ssh -o \'StrictHostKeyChecking no\' hello@192.168.80.11 \'sudo sysctl -w vm.drop_caches=3\', stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=0, shell=True) > hello@192.168.80.11's password: what I tried many times like enter password, but it failed. I just want to use ps.stdin.write(password) to send password, but it always jump password prompt immediately. How to solve this -- https://mail.python.org/mailman/listinfo/python-list
Re: python3 subprocess run sudo cmd in remote failed
On 17Sep2019 12:13, lampahome wrote: Hello, I use python3.5 and found no way to solve this problem from subprocess import Popen, PIPE ps = Popen('ssh -o \'StrictHostKeyChecking no\' hello@192.168.80.11 \'sudo sysctl -w vm.drop_caches=3\', stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=0, shell=True) hello@192.168.80.11's password: what I tried many times like enter password, but it failed. I just want to use ps.stdin.write(password) to send password, but it always jump password prompt immediately. Well, there's a Python library called "paramiko" which implements ssh. That might help. But really you should avoid using password authentication altogether. Generate a public/private keypair using ssh-keygen and install the public half on the target machine. Then all you need to do is to deal with sudo's password. Note also that since stdin and stdout are pipes and not the terminal then ssh will not be interactive, and will not allocate a tty at the far end either. You can get ssh to open a remote tty with the -t option. But I suspect you don't want stdin=PIPE or stdout=PIPE at all. Why are they there? Also, try doing this without shell=True - it is an invitation for mistakes and shell injection (depending on your uses). Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: python3 subprocess run sudo cmd in remote failed
> > Well, there's a Python library called "paramiko" which implements ssh. > That might help. > > Later I will try lol. > Note also that since stdin and stdout are pipes and not the terminal > then ssh will not be interactive, and will not allocate a tty at the far > end either. You can get ssh to open a remote tty with the -t option. > > But I suspect you don't want stdin=PIPE or stdout=PIPE at all. Why are > they there? > > I thought I can use ps.stdin.write(password), so I make stdin and stdout be pipe as input and output. Here are I tried: >from subprocess import Popen, PIPE >ps = Popen('ssh -o \'StrictHostKeyChecking no\' hello@192.168.80.11 \'sudo sysctl -w vm.drop_caches=3\', shell=True) > hello@192.168.80.11's password: >from subprocess import Popen, PIPE >ps = Popen(['ssh', '-o \'StrictHostKeyChecking no\'', ' hello@192.168.80.11', '\'sudo sysctl -w vm.drop_caches=3\'']) > hello@192.168.80.11's password: It always prompt immediately, that make me hard to enter password. Maybe I should try paramiko... -- https://mail.python.org/mailman/listinfo/python-list
Re: python3 subprocess run sudo cmd in remote failed
On 17Sep2019 13:02, lampahome wrote: Note also that since stdin and stdout are pipes and not the terminal then ssh will not be interactive, and will not allocate a tty at the far end either. You can get ssh to open a remote tty with the -t option. But I suspect you don't want stdin=PIPE or stdout=PIPE at all. Why are they there? I thought I can use ps.stdin.write(password), so I make stdin and stdout be pipe as input and output. The trouble here is that ssh will only accept a password from a terminal. As soon as you connect a pipe it refuses to prompt. This is partly security (having a terminal is a proxy for "talking to a human"), and partly because ssh normally passes stdin to the remote process once authentication is complete, so things get fiddly. You can give it a terminal by obtaining a pty and associating the subprocess with that. You could install the pexpect module with "pip install pexpect" and use that to manage this interaction. See the docs: https://pexpect.readthedocs.io/en/stable/ for further info. However, I repeat my recommendation to use a keypair for the authentication, as it avoids needing interactive passwords (and having your programme know the password has its own suite of problems to do with where that password comes from). Here are I tried: from subprocess import Popen, PIPE ps = Popen('ssh -o \'StrictHostKeyChecking no\' hello@192.168.80.11 \'sudo sysctl -w vm.drop_caches=3\', shell=True) hello@192.168.80.11's password: from subprocess import Popen, PIPE ps = Popen(['ssh', '-o \'StrictHostKeyChecking no\'', ' hello@192.168.80.11', '\'sudo sysctl -w vm.drop_caches=3\'']) hello@192.168.80.11's password: It always prompt immediately, that make me hard to enter password. Well ssh will be connected to your terminal. Do things work if you hand type the password at that point? Maybe I should try paramiko... Or pexpect. But use a keypair - it will simplify your life, and generally be far more secure anyway. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: python3 subprocess run sudo cmd in remote failed
On Tue, Sep 17, 2019 at 3:25 PM Cameron Simpson wrote: > However, I repeat my recommendation to use a keypair for the > authentication, as it avoids needing interactive passwords (and having > your programme know the password has its own suite of problems to do > with where that password comes from). Agreed; using a password that's embedded into the script is worse than useless. The same goes for sudo passwords, if that's a thing; arrange it so the user that you SSH in as has the power to run that command without a password (you can do that in the sudoers file, even if it needs a password for all other usage). If necessary, create a dedicated SSH keypair *just* for this script. It's still easier to protect an SSH private key than a password. ChrisA -- https://mail.python.org/mailman/listinfo/python-list