Created
January 27, 2022 20:57
-
-
Save fmitha/6b0135539ff00003e65afed5cdabd5a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Title: Attempting to use interact produces TypeError: write() argument must be str, not bytes | |
The following Python script using `pexpect` connects to the POP3 | |
server `pop3.mailbox.org`. It does not download any email. It will | |
most likely work on any server. | |
```python3 | |
#!/usr/bin/python3 | |
# Connection variables: please include your own values | |
ip = "pop3.mailbox.org" | |
socket = "995" | |
user = xxxxxx | |
password = xxxxxx | |
import pexpect | |
import sys | |
try: | |
cmd = f'openssl s_client -connect {ip}:{socket} -quiet' | |
child = pexpect.spawn(cmd, timeout=5, encoding='utf-8') | |
child.logfile_read = sys.stdout | |
child.expect(f'\+OK.*') | |
child.sendline(f'user {user}') | |
child.expect(f'\+OK.*') | |
child.sendline(f'pass {password}') | |
child.expect(f'\+OK.*') | |
child.interact() | |
except: | |
print("Exception was thrown") | |
print("debug information:") | |
print(str(child)) | |
raise | |
``` | |
Here is the output. | |
```none | |
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root G2 | |
verify return:1 | |
depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = Thawte TLS RSA CA G1 | |
verify return:1 | |
depth=0 CN = *.mailbox.org | |
verify return:1 | |
+OK Dovecot ready. | |
user xxxxxxxxx | |
+OK | |
pass xxxxxxxxx | |
+OK Logged in. | |
LIST | |
+OK 9 messages: | |
1 134551 | |
2 2599 | |
3 2371 | |
4 3341 | |
5 2539 | |
6 2541 | |
7 2122 | |
8 7185 | |
9 7174 | |
. | |
``` | |
and I press any key, I get a bunch of diagnostic information from | |
`print(str(child))`, followed by the following traceback: | |
```none | |
Traceback (most recent call last): | |
File "/tmp/pexpect.txt", line 31, in <module> | |
child.interact() | |
File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 793, in interact | |
self.__interact_copy(escape_character, input_filter, output_filter) | |
File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 838, in __interact_copy | |
self._log(data, 'read') | |
File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 133, in _log | |
second_log.write(s) | |
TypeError: write() argument must be str, not bytes | |
``` | |
I thought the `encoding='utf-8'` argument to the `spawn` command was | |
supposed to take care of that issue. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment