Is there any way to have socket.socket.recv
run with hidden input. For example, if I was asking for a password I would want the input to be hidden, as if I were running the "sudo" bash command.
Edit:
socket.socket.recv
asks for data on the remote end. When you are connected to the server it will ask you for text and when you type it in it will be shown in your console. Now when you use the sudo command it asks for the password and you can't see the text you type. I want something like this that would work for socket.sock.recv
, so you wouldn't see the password you type.
Edit 2:
When I said socket.socket.recv
I actually meant something like socket._socketobject.recv
which would look like this in my program: client.recv(BUF_SIZE)
.
Edit 3:
I am making a telnet server. client.recv(BUF_SIZE)
is like running raw_input
on the clients computer. So is there anything that is similar to running getpass.getpass
on the clients computer?
views:
245answers:
3socket.recv()
returns data from a socket, what you do with that data is up to you.
I guess you are doing something like this:
s.connect(...)
while True:
print s.recv(4096)
In which case your problem is the remote end where someone is presumably typing input.
Can you clarify your question please? recv()
never displays data by itself.
(In response to the questioner's clarification)
If you want to be able to obtain input from the user at a console (text-mode) prompt without it being displayed, you probably want to use the getpass
.
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from getpass import getpass
>>> p = getpass("Enter some text now: ")
Enter some text now:
>>> print p
secret
>>>
However, this has absolutely nothing to do with sockets and networking. The variable p
above contains the user's entered text (i.e. their password). If you send this down a network with socket.sendall(p)
the remote end will receive this data. At that point it is up to the receiving script to decide what to do with the data...
(In response to edit 2)
Here is a simple example of how you can encode whether some data is secret or not, and pass that data between functions:
Python 2.6.2 (r262:71600, Sep 22 2009, 18:29:26)
[GCC 3.4.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def capture(secret=False):
... if secret:
... from getpass import getpass
... return chr(0) + getpass("Enter some secret text now: ")
... else:
... return raw_input("Enter some text now: ")
...
>>> def display(data):
... if data[0] == chr(0):
... print "(Secret text hidden)"
... else:
... print data
...
>>> display( capture() )
Enter some text now: Hello
Hello
>>> display( capture(secret=True) )
Enter some secret text now:
(Secret text hidden)
>>>
If you are using sockets, you will need to use s.sendall( capture() )
at one end, and display( s.recv4096) )
at the other.