2010-11-18 3 views
1

저는 SocketServer를 사용하여 파이썬으로 만든 간단한 서버 응용 프로그램을 가지고 있습니다.이 서버는 매우 원시적 인 명령 행 유형 입력 시스템을 가지고 있습니다. 내 주요 문제는 여기에 서버가 메시지를 받으면 화면에 인쇄한다는 것입니다. raw_input 함수가 텍스트가 입력되고 검사 될 때까지 기다리는 것을 제외하면 이것은 모두 훌륭합니다. 서버 handle() 함수에서 raw_input을 중지 시키거나 입력을 끝내고 서버가받는 정보를 표시하는 예외를 발생시키는 방법이 있습니까?다른 텍스트가 표시되면 raw_input()을 종료합니다.

감사합니다.
잭.

+1

을 이런 종류의 일을 위해서? –

답변

1

내가 아는 한, raw_input은 python 명령 콘솔에서 입력을 받아들이 기 때문에 이것은 불가능합니다. 그러나 예상치 못한 주위를 둘러 쌀 수있는 몇 가지 방법이 있습니다.

1 - 콘솔을 사용하는 대신 출력 및 입력 줄이있는 간단한 Tkinter 창을 만듭니다. 고정 폭 글꼴을 사용하는 스크롤 막대에있을 수있는 창 텍스트 끝에 메시지를 추가하는 사용자 지정 인쇄 기능을 만든 다음 입력에 응답하는 명령 프롬프트 상자를 만듭니다. 그 코드는 다음과 같이 보일 것입니다 :

from Tkinter import * 
root = Tk() 
topframe=Frame(root) 
bottomframe=Frame(root) 
bottomframe.pack(side=BOTTOM,fill=X) 
topframe.pack(side=TOP,fill=BOTH) 
scrollbar = Scrollbar(topframe) 
scrollbar.pack(side=RIGHT,fill=Y) 
text = Text(topframe,yscrollcommand=scrollbar.set) 
text.pack(side=LEFT,fill=BOTH) 
scrollbar.config(command=text.yview) 
text.config(state=DISABLED) 
v = StringVar() 
e = Entry(bottomframe,textvariable=v) 
def submit(): 
    command = v.get() 
    v.set('') 
    #your input handling code goes here. 
    wprint(command) 
    #end your input handling 
e.bind('<Return>',submit) 
button=Button(bottomframe,text='RUN',command=submit) 
button.pack(side=RIGHT) 
e.pack(expand=True,side=LEFT,fill=X) 
def wprint(obj): 
    text.config(state=NORMAL) 
    text.insert(END,str(obj)+'\n') 
    text.config(state=DISABLED) 
root.mainloop() 

또 다른 옵션은 다음과 같이보고 자신의 인쇄 및 raw_input을 방법을 생성하는 것입니다 : 왜 당신이 GUI 프레임 워크를 사용하지 않는

import threading 
wlock=threading.Lock() 
printqueue=[] 
rinput=False 
def winput(text): 
    with wlock: 
     global printqueue,rinput 
     rinput=True 
     text = raw_input(text) 
     rinput=False 
     for text in printqueue: 
      print(text) 
     printqueue=[] 
     return text 
def wprint(obj): 
    global printqueue 
    if not(rinput): 
     print(str(obj)) 
    else: 
     printqueue.append(str(obj)) 
관련 문제