views:

245

answers:

3

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?

+1  A: 

socket.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.

RobM
I meant that `socket.socket.recv` displays data when you type it in on the client end, because whatever you type is shown (in the client end). Now when you use the `sudo` command whatever you type for the password isn't shown. That's an example of hidden input. I was wondering if there was some kind of recv where the input (on the client end) is hidden (not shown when it is typed).
None
+1  A: 

(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...

RobM
This would ask for hidden input on the server end. I want something like this that would ask for hidden input on the REMOTE end. Something that would act just like `client.recv` but the input would be hidden.
None
You misunderstand sockets - they are just a mechanism for sending data from host to host. You are concerned with collecting and displaying input to a *human*. Your server and client should handle how data is captured and displayed: it has *nothing* to do with sockets.
RobM
+1  A: 

(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.

RobM