具有隐藏输入的 python socket.socket.recv

发布于 2024-09-01 20:52:22 字数 642 浏览 8 评论 0原文

有没有办法让 socket.socket.recv 使用隐藏输入运行。例如,如果我要求输入密码,我希望隐藏输入,就像我正在运行“sudo”bash 命令一样。
编辑:
socket.socket.recv 请求远程端的数据。当您连接到服务器时,它会要求您输入文本,当您输入文本时,它将显示在控制台中。现在,当您使用 sudo 命令时,它会要求输入密码,并且您看不到您键入的文本。我想要这样的东西,适用于 socket.sock.recv,这样您就看不到您输入的密码。
编辑2:
当我说 socket.socket.recv 时,我实际上指的是 socket._socketobject.recv 之类的东西,它在我的程序中看起来像这样: client.recv(BUF_SIZE)
编辑3:
我正在制作一个 telnet 服务器。 client.recv(BUF_SIZE) 就像在客户端计算机上运行 raw_input 一样。那么有什么类似于在客户端计算机上运行 getpass.getpass 的东西吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不喜欢何必死缠烂打 2024-09-08 20:52:22

socket.recv() 从套接字返回数据,您如何处理该数据取决于您。

我猜你正在做这样的事情:

s.connect(...)
while True:
  print s.recv(4096)

在这种情况下,你的问题是远程端,可能有人正在输入输入。

您能澄清一下您的问题吗? recv() 从不单独显示数据。

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.

零度° 2024-09-08 20:52:22

(针对提问者的澄清)

如果您希望能够在控制台(文本模式)提示符下获取用户的输入而不显示它,您可能需要使用 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
>>> 

然而,这与套接字和网络完全无关。上面的变量p包含用户输入的文本(即他们的密码)。如果您使用 socket.sendall(p) 将其发送到网络,远程端将收到此数据。此时,由接收脚本决定如何处理数据......

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

难忘№最初的完美 2024-09-08 20:52:22

(响应编辑2)

这是一个简单的示例,说明如何对某些数据是否保密进行编码,并在函数之间传递该数据:

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)
>>>

如果您使用套接字,则需要使用 s.sendall( capture() ) 位于一端,display( s.recv4096) ) 位于另一端。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文