2011-07-14 8 views
2

私は、小さなサーバーとクライアントを楽しく学び、ソケットの使い方と簡単なネットワークのやり方を学んでいます。 Telnetの「プロトコル」を使用してサーバーにユーザー名を要求し、次にパスワードを入力します。サーバは、ユーザ名として「NICK」を取得した場合、次のように送信されます。connection.write(bytes("NICK\n", 'latin1'))接続はTelnetオブジェクトです。それは "整数引数が予想され、浮動小数点を取得しました"とクライアントがそれに文句を言うPythonのSocketerverからTelnetへのエラー

何が起こっていたのですか?

ここでは、クライアントとサーバーの完全なコードがあります:


クライアント:

# 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

あなたのコードはすべて実際には1つのファイルにありますか(私は望んでいません)?サーバーを起動してからクライアントを起動しますか? –

+1

いいえ、1つのファイルにはありません。それらは別々ですが、StackOverflowのコードセレクタはそれらをまとめました。 – Nick

答えて

0

Python3を使用しているので、 "login:"はUnicode文字列ですが、connection.read_untilはバイトとして解釈できるものを期待しています。お試しください

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

さて、実際にサーバーがクライアントから受け取ったものをチェックしました。それはこれです:27569568recv、実際に送信したものとは関係ありません。あまり意味がない。 – Nick

関連する問題