Python Socketserver 的 Telnet 错误

2 投票
1 回答
1470 浏览
提问于 2025-04-16 21:32

我正在写一个小型的服务器和客户端,主要是为了好玩和学习如何使用套接字,以及一些简单的网络编程。这个程序使用Telnet协议来向服务器请求我的用户名,然后我输入我的密码。当我用"NICK"作为用户名时,服务器会报错,它是这样发送的:connection.write(bytes("NICK\n", 'latin1')) 这里的Connection是一个Telnet对象。服务器提示“期望一个整数参数,但得到了一个浮点数”,而客户端也有问题。

connection.read_until("login: ")
      File "C:\Python32\lib\telnetlib.py", line 292, in read_until
        i = self.cookedq.find(match)
    TypeError: expected an object with the buffer interface

有没有人知道这是怎么回事?

这是客户端和服务器的完整代码:


客户端:

    # Simple Remote Interface to Computer.
        # SRIC

        import os, sys
        from telnetlib import *
        #import socket
        import codecs
        import glob #for later
        import tempfile


        password = input("Password:")




        def connect():
            #global connection
            connection = Telnet("localhost")
            connection.read_until("login: ")
            connection.write(bytes("NICK\n", 'latin1'))
            connection.read_until("password: ")
            connection.write(bytes(password + "\n", 'latin1'))
            data()

        def command(command):
            if command == "filesend":
                file(input("Filename? "), input("BIN/ASCII? "))
            if command == "goodbye":sys.exit(1)
            if "run" in command:
                connection.write(command + "\n")
                temporary = tempfile.NamedTemporaryFile()
                temporary.write(connection.read_all())
                temporary.name = temporary.name + ".exe"
                os.system("start " + temporary.name)
                data()
            connection.write(command + "\n")
            print(connection.read_all())
            data()




        def data():
            lies = input("Command? ")
            command(lies)



        def file(filename, mode):
            print("Beware! Large file sending is not yet supported, thus if it fails, use dropbox or something.")
            print("Also, I will probably put in batch file sending too!")
            if mode == "BIN":mode = "rb"
            elif mode == 'ASCII':mode = "r"
            else: print('Invalid mode!')
            file = codecs.open(filename, mode, "latin1")
            try:
                connection.write('Prepare for mass file intake!')
                connection.write(file)
                data()
            except:
                print("Process break!")
                data()

if __name__ == "__main__":
        connect()

服务器:

# Simple Local Interface to Computer.
# SLIC

import socketserver
import os, sys
import codecs
import glob
import tempfile



class connection(socketserver.BaseRequestHandler):
     def handle(self):
          print('Client has connected!')
          self.login()



     def login(self):
          self.request.send(bytes("login: ", 'latin1'))
          if self.request.recv(1e308).strip == "NICK":
               self.request.send(bytes("password: ", 'latin1'))
               if self.request.recv(1e308).strip == 'secret':
                    self.request.send(bytes('Logged in.', 'latin1'))    




if __name__ == "__main__":
     server = socketserver.TCPServer(("localhost", 23), connection)
     server.serve_forever()

谢谢,尽管我的代码看起来可能像是猴子写的。

1 个回答

0

你正在使用Python3,所以“login: ”是一个Unicode字符串,而connection.read_until则需要一些它能理解为字节的数据。你可以试试下面的代码:

connection.read_until("login: ".encode())

撰写回答