Python 3 论坛列表

Python 3 论坛列表

我有以下脚本。它与 telnet(甚至 ztelnet)完美配合,问题是它不允许我用另一个术语回答所有问题。除了 telnet 或 ztelnet 之外。因此,我远程登录到 BBS 并通过服务器运行此脚本,使用我的终端程序 (igTerm) 和此远程登录客户端:https://github.com/seamiddleditch/libtelnet(telnet 客户端)。我正在尝试让它与 igTerm 或 telnet-client 一起使用。我无法弄清楚这一点。和显示例程有关系。另外,由于某种奇怪的原因,它在 Linux 控制台上运行得非常好。所以,无论如何,这是脚本。是的,我知道这里有一些“脏代码”。 :)

#!/usr/bin/env python3

from os import system
import curses
import readline
import sys
import os
import os.path
import time
from math import *

from sites import sites

commands = ["\n\n\n add           ", " connect       ", " information   ", " help         ", " quit          "];

def pause():
     programPause = input("[16;5H[1;37mpress <[0;37mreturn[1;37m>[0;37m                [16;19H")

def execute_cmd(cmd_string):
        system("clear");
        a = system(cmd_string);

path = 'bbses/'
isExist = os.path.exists(path)
if not isExist:

  os.makedirs(path)
  print("The new directory is created!")
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
screen.keypad( 1 )
curses.init_pair(1,curses.COLOR_WHITE, curses.COLOR_BLUE)
highlightText = curses.color_pair( 1 )
normalText = curses.A_NORMAL
max_row = 10 #max number of rows
max_row2 = 5
box = curses.newwin(0,0,5,2)

option = 0;

row_num = len( sites )
row_num2 = len( commands )

pages = str( row_num / max_row )
position = 1
page = 1
for i in range( 1, max_row + 1 ):
         if (i == position):
             box.addstr( i, 2, str( i ) + " - " + sites[ i - 1 ][0], highlightText )
         else:
             box.addstr( i, 2, str( i ) + " - " + sites[ i - 1 ][0], normalText )
         if i == row_num:
             break

pages2 = int( ceil( row_num2 / max_row2 ) )
position2 = 1
page2 = 1
box.addstr("\n\n\n\n\n")
for j in range( 1, max_row2 + 1 ):
         if (j == position2):
             box.addstr(commands[ j - 1 ], highlightText )
         else:
             box.addstr(commands[ j - 1 ], normalText )
         if j == row_num2:
             break


screen.refresh()
box.refresh()

os.system("cat bbslist.ans")

x = screen.getch()

while x != ord('q') and x != ord('Q'):  # Enter in ascii

     if x == curses.KEY_DOWN:
         if page == 1:
             if position < i:
                 position = position + 1
                 option = option + 1
             else:
                     page = page + 1
                     position = 1 + ( max_row * ( page - 1 ) )
         elif page == pages:
             if position < row_num:
                 position = position + 1
         else:
             if position < max_row + ( max_row * ( page - 1 ) ):
                 position = position + 1
             else:
                 page = page + 1
                 position = 1 + ( max_row * ( page - 1 ) )
     if x == curses.KEY_UP:
         if page == 1:
             if position > 1:
                 position = position - 1
                 option = option - 1
         else:
             if position > ( 1 + ( max_row * ( page - 1 ) ) ):

                 position = position - 1
             else:
                 page = page - 1
                 position = max_row + ( max_row * ( page - 1 ) )

     if x == curses.KEY_RIGHT:
         if page2 == 1:
             if position2 < j:
                 position2 = position2 + 1
             else:
                 if pages2 > 1:
                     page2 = page2 + 1
                     position2 = 1 + ( max_row2 * ( page2 - 1 ) )
         elif page == pages:
             if position2 < row_num2:
                 position2 = position2 + 1
         else:
             if position2 < max_row2 + ( max_row2 * ( page2 - 1 ) ):
                 position2 = position2 + 1
             else:
                 page2 = page2 + 1
                 position2 = 1 + ( max_row2 * ( page2 - 1 ) )
     if x == curses.KEY_LEFT:
         if page2 == 1:
             if position2 > 1:
                 position2 = position2 - 1
         else:
             if position2 > ( 1 + ( max_row2 * ( page2 - 1 ) ) ):

                 position2 = position2 - 1
             else:
                 page = page - 1
                 position2 = max_row2 + ( max_row2 * ( page2 - 1 ) )

     if x == ord( "\n" ) and row_num != 0 and row_num2 != 0:


         if x == 10 and position2 == 1:
                curses.endwin();
                print("[H[7;5H[1;30m+---------------------------------------------------------------+[0;37m")
                print("[8;5H[1;30m  [1;34mbbs name[1;30m       :                                   [1;37m  ")  
                print("[9;5H[1;30m  [1;34msysop name[1;30m    :                                   [1;37m  ")
                print("[10;5H[1;30m  [1;34mtelnet or ssh[1;30m :                                  [1;37m ")
                print("[11;5H[1;30m  [1;34mbbs address[1;30m   :                                   [1;37m  ")
                print("[12;5H[1;30m  [1;34mbbs software[1;30m  :                                   [1;37m  ")
                print("[13;5H[1;30m  [1;34mbbs theme[1;30m     :                                   [1;37m  ")
                print("[14;5H[1;30m  [1;34mother info[1;30m    :                                   [1;37m  ")          
                print("[15;5H[1;30m+---------------------------------------------------------------+[0;37m")

                bbsname = input("[8;5H[1;30m  [1;34mbbs name[1;30m      : [1;37m  ")
                type(bbsname);
                sysopname = input("[9;5H[1;30m  [1;34msysop name[1;30m    : [1;37m  ")
                type(sysopname);
                protocol = input("[10;5H[1;30m  [1;34mtelnet or ssh[1;30m : [1;37m  ")
                type(protocol)
                address = input("[11;5H[1;30m  [1;34mbbs address[1;30m   : [1;37m  ")
                type(address);
                software = input("[12;5H[1;30m  [1;34mbbs software[1;30m  : [1;37m  ")
                type(software);
                bbstheme = input("[13;5H[1;30m  [1;34mbbs theme[1;30m     : [1;37m  ")
                type(bbstheme);
                otherinfo = input("[14;5H[1;30m  [1;34mother info[1;30m    : [1;37m  ")
                type(otherinfo);
                print("[15;5H[1;30m+---------------------------------------------------------------+[0;37m")
                save_path = 'bbses/'
                completeName = os.path.join(save_path, address)
                saveFile = open(completeName,"a")
                file = open("sites.py", "a+")
                saveFile.write("[H[7;5H[1;30m+---------------------------------------------------------------+[0;37m")
                saveFile.write("[8;5H[1;30m  [1;34mbbs name[1;30m      : [1;37m  ")
                saveFile.write(bbsname)
                saveFile.write("\n")
                saveFile.write("[9;5H[1;30m  [1;34msysop name[1;30m    : [1;37m  ")
                saveFile.write(sysopname)
                saveFile.write("\n")
                saveFile.write("[10;5H[1;30m  [1;34mtelnet or ssh[1;30m : [1;37m  ")
                saveFile.write(protocol)
                saveFile.write("\n")
                saveFile.write("[11;5H[1;30m  [1;34mbbs address[1;30m   : [1;37m  ")
                saveFile.write(address)
                saveFile.write("\n")
                saveFile.write("[12;5H[1;30m  [1;34mbbs software[1;30m  : [1;37m  ")
                saveFile.write(software)
                saveFile.write("\n")
                saveFile.write("[13;5H[1;30m  [1;34mbbs theme[1;30m     : [1;37m  ")
                saveFile.write(bbstheme)
                saveFile.write("\n")
                saveFile.write("[14;5H[1;30m  [1;34mother info[1;30m    : [1;37m  ")
                saveFile.write(otherinfo)
                saveFile.write("\n")
                saveFile.write("[15;5H[1;30m+---------------------------------------------------------------+[0;37m")
                saveFile.close()
                system("perl -00pe 's/](?!.*])//s' sites.py > sites")
                system("cat sites.py | sed -e 's/];//g' > sites")
                system("mv sites sites.py")
                file = open("sites.py", "a")
                file.write(',')
                file.write('[')
                file.write('"')
                file.write(bbsname)
                file.write('", "')
                file.write(protocol)
                file.write('", "')
                file.write(address)
                file.write('"')
                file.write(']];')
                file.close()


         elif x == 10 and position2 == 2:
                i = position;
                screen.clear();
                os.system("stty raw");
                execute_cmd(sites[i - 1][1] + " " + sites[i - 1][2]);
                curses.endwin();

         if x == 10 and position2 == 3:
                i = position;
                os.system("cat " + "bbses/" + sites[i - 1][2]);
                pause()
                curses.endwin()

         if x == 10 and position2 == 4:
                system("cat bbslisthelp.ans")
                pause()
                curses.endwin()
         if x == 10 and position2 == 5:
                quit(1);


     box.erase()
     screen.border(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ')

     for i in range( 1 + ( max_row * ( page - 1 ) ), max_row + 1 + ( max_row * ( page - 1 ) ) ):
         if row_num == 0:
             box.addstr( 1, 1, "There aren't strings",  highlightText )
         else:
             if ( i + ( max_row * ( page - 1 ) ) == position + ( max_row * ( page - 1 ) ) ):
                 box.addstr( i - ( max_row * ( page - 1 ) ), 2, str( i ) + " - " + sites[ i - 1 ][0], highlightText )
             else:
                 box.addstr( i - ( max_row * ( page - 1 ) ), 2, str( i ) + " - " + sites[ i - 1 ][0], normalText )
             if i == row_num:
                 break
             box.addstr("\n\n\n\n\n")
     for j in range( 1 + ( max_row2 * ( page2 - 1 ) ), max_row2 + 1 + ( max_row2 * ( page2 - 1 ) ) ):
             if ( j + ( max_row2 * ( page2 - 1 ) ) == position2 + ( max_row2 * ( page2 - 1 ) ) ):
                 box.addstr(commands[ j - 1 ], highlightText )
             else:
                 box.addstr(commands[ j - 1 ], normalText )
             if j == row_num2:
                 break

     screen.refresh()
     box.refresh()
     os.system("cat bbslist.ans")
     x = screen.getch()

curses.endwin()
exit()

相关内容