这是一个使用netcat(ncat来自Nmap)的假telnet服务器:$ ncat -l 9000 < msg.txt > log.txt
在端口9000上列出并传递一个名为msg.txt(问题)的文件并将输入记录到log.txt(答案)中,它应该模拟您的服务器。在
文件msg.txt内容:
^{pr2}$
以十六进制表示的文件内容(使用hexdump msg.txt):
^{3}$
注意新行字符,它是\x0A或{}(它也可以是\x0D\x0A或{})。在
客户:import telnetlib
port = 9000
host = "127.0.0.1"
tn = telnetlib.Telnet(host, port)
r = tn.read_until("""\nWelcome to EULER!
=================
Answer 100 simple questions to authenticate yourself\n""")
tn.read_until("What is your name?\n")
tn.write("foo\n") # The client sends `\n`, notice the server may expects `\n\r`.
print("Question 1 answered.")
tn.read_until("How old are you?\n")
tn.write("100\n")
print("Question 2 answered.")
tn.read_until("Do you use Python?\n")
tn.write("yep\n")
print("Question 3 answered.")
tn.close()
现在让我们在客户端测试它:$ python client.py
Question 1 answered.
Question 2 answered.
Question 3 answered.
$
在服务器端,转储日志文件内容:$ ncat -l 9000 < msg.txt > log.txt
$
$ cat log.txt # or `type log.txt` on windows
foo
100
yep
$
$ hexdump log.txt
00000000: 66 6F 6F 0A 31 30 30 0A 79 65 70 0A |foo 100 yep |
0000000c;
$
把它放在一起,你应该会明白的。在