Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将两个脚本作为一个脚本运行_Python_Python 2.7 - Fatal编程技术网

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

Python 将两个脚本作为一个脚本运行,python,python-2.7,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"

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

发送脚本:

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)
接收脚本:

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)
注意:这两个脚本必须在两台电脑上运行。一个接收并显示消息,另一个发送消息

以下是我的尝试:

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)
从系统导入退出
LAN\u NAME=“\\\\ONE-PC”
LAN\u MSG\u FILE=“%s\\Users\\Public\\me.txt”%LAN\u NAME
CLN=“\\\\TWO-PC”
CLN_MSG_FILE=“%s\\Users\\Public\\me.txt”%CLN
TCHAT_VER=“1.0.2”
def fetch_数据(src):
文件=打开(src,'r')
文件\数据=文件\读取()
文件u.close()
返回文件\u数据
def发送_数据(src,数据):
文件=打开(src,'w')
文件\ truncate()
文件写入(数据)
文件u.close()
def清洁_缓存(src):
文件=打开(src,'w')
文件\ truncate()
文件u.close()
打印“TChat-版本%s”%TChat\u版本
打印“版权(c)2013-Ahnaf Tahmid”
打印“-------------------------------------”
打印“\n”
打印“要随时退出,请键入[退出]”
打印“\n”
msg=原始输入(“[您]:”)
msg_lan=“”
而味精!=“[退出]”:
发送数据(局域网消息文件,消息)
打印“\n”
打印“获取数据…”
msg\u lan=获取数据(CLN\u msg\u文件)

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

基本设计模式可以是:

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

在主线程上,创建接收器并启动它,然后进入输入循环:如果用户输入消息,则在同一线程上发送消息。如果用户键入“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
类接收器(线程):
定义初始化(自):
超级(接收器,自我)。\uuuu初始化
self.cancelled=False
def运行(自):
虽然未自行取消:
msg=nonBlockingReceive()#您的非阻塞接收函数
如果消息:打印(“>”)
如果msg==“退出”:
receiver.cancelled=True
receiver.wait()
打破
sendData(msg)#您的发送功能
(只是未经测试的伪代码)