不接收使用python套接字从服务器返回的消息

时间:2020-09-25 02:32:21

标签: python sockets networking netcat

主持人: 45.77.245.244
端口: 5006

它是CTF挑战赛的主持人。但是我不需要解决它。我想在python中自动化它。

连接到服务器时。服务器将返回2条消息

1:编程.....

2:n =? \ NAnwser:

并听取用户输入的答案。

Netcat:

toor@MSI:~$ nc 45.77.245.244 5006

PROGRAMING - WHITEHAT 2019:

--> Coungting the triangles <--

HOW MANY TRIANGLE IS CREATED BY N (1..N) NUMBER. N < 10^6

Example: with N = 5
OUTPUT : 3

(2,3,4),(3,4,5),(2,4,5)
................/\...................|\...................
.............../  \..................| \..................
............../    \.................|  \.................
............./      \................|   \................
............/        \...............|    \...............
.........../          \..............|     \..............
........../____________\.............|______\.............

n = 99
Answer: 4
Wrong answer. I can't trust you anymore :'<

我写了一段代码,将其连接并发送回“ n”并获得所需的结果:
Wrong answer. I can't trust you anymore: '<

我的Python代码:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('45.77.245.244', 5006))

print('Server:', str(s.recv(1024)))
while True:
    data = s.recv(1024).strip()
    if data:
        data = str(data)
        print('Server:', data)
        n = ''.join([i for i in data if i.isdigit()])
        n = bytes(n, 'ascii')
        s.send(n)
        print('Client: ', n)
s.close()

输出:

Server: b'\nPROGRAMING - WHITEHAT 2019:\n\n--> Coungting the triangles <--\n\nHOW MANY TRIANGLE IS CREATED BY N (1..N) NUMBER. N < 10^6\n\nExample: with N = 5\nOUTPUT : 3 \n\n(2,3,4),(3,4,5),(2,4,5)\n................/\\...................|\\...................\n.............../  \\..................| \\..................\n............../    \\.................|  \\.................\n............./      \\................|   \\................\n............/        \\...............|    \\...............\n.........../          \\..............|     \\..............\n........../____________\\.............|______\\.............\n\t\n'
Server: b'n = 25\nAnswer:'
Client:  b'25'

send(n)print('send:', n)确定发送成功之后,使用 netcat 时我没有得到想要的输出。 / p>

输出:Wrong answer. I can't trust you anymore :'<

我哪里出错了?有办法解决吗?

1 个答案:

答案 0 :(得分:0)

也许这是一个简单的问题。该代码似乎没有任何错误,但现实是我忘记发送字符“ \ n”。我如何解决:

n = (n + '\n')
s.send(n)

在这里,我需要添加一个换行符^^