将两个脚本作为一个脚本运行

时间:2013-08-01 19:25:31

标签: python python-2.7

如果启用了文件和打印机共享,我已经制作了一个在局域网内工作的聊天程序。它基于简单的纯文本文件。有两种不同的脚本。一个处理发送部分,另一个处理接收部分。我试图把它作为一个脚本但是必须输入一条消息来接收另一个用户发送的消息。那么,我如何把它作为一个脚本?

发送脚本:

from sys import exit

LAN_NAME = "\\\\ONE-PC"
LAN_MSG_FILE = "%s\\Users\\Public\\me.txt" % LAN_NAME
TCHAT_VER = "1.1.0"

print "TChat - Version %s" % TCHAT_VER
print "Copyright(c) 2013 - Ahnaf Tahmid"
print "--------------------------------"

def send_data(src, msg):
    _file_ = open(src, 'w')
    _file_.truncate()
    _file_.write(msg)
    _file_.close()

msg = raw_input("[You]: ")
print "\n"

while True:
    send_data(LAN_MSG_FILE, msg)
    msg = raw_input("[You]: ")
    print "\n"
    if msg == "[exit]":
        send_data(LAN_MSG_FILE, "[exit]")

raw_input("Press ENTER to exit.")
exit(0)

接收脚本:

from sys import exit

CLN = "\\\\TWO-PC"
CLN_MSG_FILE = "%s\\Users\\Public\\me.txt" % CLN
VER = "1.0.0"

def fetch_data(src):
    _file_ = open(src, 'r')
    message = _file_.read()
    _file_.close()
    return message

msg = " "
msg_to_display = "x"

print "TChat Receiver v%s." % VER
print "Copyright(c) 2013 - Ahnaf Tahmid."
print "---------------------------------"
print "\n"

while True:
    msg = fetch_data(CLN_MSG_FILE)
    if msg_to_display == msg:
        pass
    else:
        msg_to_display = msg
        if msg_to_display == "[exit]":
            print "[%s] has disconnected.";
            raw_input("Press ENTER to exit")
            exit(0)
        print "[%s]: %s\n" % (CLN, msg_to_display)

注意:必须在两台PC上运行这两个脚本。一个接收消息并显示它们,另一个发送它们。

以下是我尝试将它组合在一起的方法:

from sys import exit

LAN_NAME = "\\\\ONE-PC"
LAN_MSG_FILE = "%s\\Users\\Public\\me.txt" % LAN_NAME
CLN = "\\\\TWO-PC"
CLN_MSG_FILE = "%s\\Users\\Public\\me.txt" % CLN
TCHAT_VER = "1.0.2"

def fetch_data(src):
    file_ = open(src, 'r')
    file_data = file_.read()
    file_.close()
    return file_data

def send_data(src, data):
    file_ = open(src, 'w')
    file_.truncate()
    file_.write(data)
    file_.close()

def clean_cache(src):
    file_ = open(src, 'w')
    file_.truncate()
    file_.close()

print "TChat - Version %s" % TCHAT_VER
print "Copyright(c) 2013 - Ahnaf Tahmid"
print "--------------------------------"
print "\n"
print "To exit anytime, type in [exit]"
print "\n"

msg = raw_input("[You]: ")
msg_lan = " "

while msg != "[exit]":
    send_data(LAN_MSG_FILE, msg)
    print "\n"
    print "Fetching data.."
    msg_lan = fetch_data(CLN_MSG_FILE)
    while len(msg_lan) <= 1:
        msg_lan = fetch_data(CLN_MSG_FILE)
    clean_cache(CLN_MSG_FILE)
    print "\n"
    print "[Friend]: %s" % msg_lan
    print "\n"
    msg = raw_input("[You]: ")

print "\n"
print "Thanks for using TChat."
raw_input("Press ENTER to exit")
exit(0)

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您已经确定了基本问题:需要同时运行发送和接收。您必须深入研究多线程。

基本设计模式可以是:

为接收部件创建一个带有取消标志的线程。循环无限地执行以下操作:检查是否有入站数据;如果收到并输出;睡觉超时;如果设置了取消标志,则中断。

在主线程上,创建接收器并启动它,然后输入您的输入循环:如果用户输入消息,请在同一个线程上发送。如果用户键入'exit',则在接收器线程上设置取消标志,等待它并退出。

或多或少:

class Receiver (Thread):
    def __init__ (self):
        super (Receiver, self).__init__ ()
        self.cancelled = False

    def run (self):
        while not self.cancelled:
            msg = nonBlockingReceive () # your non-blocking receive function
            if msg: print ('<< {}'.format (msg) )
            self.sleep (2)

receiver = Receiver ()
receiver.start ()
while True:
    msg = input ('>> ')
    if msg == 'exit':
        receiver.cancelled = True
        receiver.wait ()
        break
    sendData (msg) # your send function

(只是未经测试的伪代码)