2

I'm writing a tiny server and client kind of for fun and learn how to use sockets, and how to do some simple networking. It uses the Telnet 'protocol' to ask the server for my username, and then I enter my password. The server complains when it gets "NICK" as a username, it was sent like this: connection.write(bytes("NICK\n", 'latin1')) Connection is a Telnet object. It says "Integer argument expected, got float" and the client complains that

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

Does anyone know what was going on?

Here's the full code for the client and the server:


Client:

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

Server:

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

Thanks, even though my code probably looks like a monkey wrote it.

Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
Nick
  • 55
  • 5
  • 1
    Is all of your code actually in a single file (I'm hoping not)? Do you launch your server, and then the client? – g.d.d.c Jul 14 '11 at 16:33
  • 1
    No, they're not in a single file. They're separate, but StackOverflow's code selector thing globbed them together. – Nick Jul 14 '11 at 16:44

1 Answers1

0

You are using Python3, so "login: " is a unicode string, while connection.read_until is expecting something it can interpret as bytes. Try

connection.read_until("login: ".encode())
stefan
  • 1,511
  • 9
  • 13
  • Well, I actually just checked what the server recieves from the client, and it's this: 27569568recv, which has nothing to do with what I actually sent. Doesn't make much sense. – Nick Jul 16 '11 at 00:37