python调用本地程序_如何通过本地消息传递chrome应用程序调用本地python程序?

我在试着调用程序“测试.py它是我硬盘上的本地文件,通过我的“原生应用程序”程序与我的chrome应用程序通信。但chrome应用程序无法运行“测试.py“当我尝试通过谷歌chrome运行应用程序时。但是通过命令行执行本机应用程序会调用“测试.py“和预期的一样。如何通过我的本地程序调用python应用程序?在

本机应用程序import struct

import sys

import threading

import Queue

from urllib2 import urlopen

import subprocess

import thread

import time

import io

import base64

import urllib

from tkFileDialog import askopenfile, asksaveasfile

#from test import main

msg="hello"

flag=1

try:

import Tkinter

import tkMessageBox

except ImportError:

Tkinter = None

# On Windows, the default I/O mode is O_TEXT. Set this to O_BINARY

# to avoid unwanted modifications of the input/output streams.

if sys.platform == "win32":

import os, msvcrt

msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)

msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

def print_time():

print 'hi'

execfile("test.py")

print 'test done'

global flag

flag=0

#print('done')

# Helper function that sends a message to the webapp.

def quitxy():

message1='hello'

#sys.stdout.write(struct.pack('I', len(message1)))

#sys.stdout.write(message1)

#sys.stdout.flush()

#print('hi')

threadx = threading.Thread(target=print_time, args=())

threadx.start()

#thready = threading.Thread(target=main,args=())

#thready.start()

def send_message(message):

# Write message size.

sys.stdout.write(struct.pack('I', len(message)))

# Write the message itself.

sys.stdout.write(message)

sys.stdout.flush()

# Thread that reads messages from the webapp.

def read_thread_func(queue):

message_number = 0

while 1:

# Read the message length (first 4 bytes).

text_length_bytes = sys.stdin.read(4)

if len(text_length_bytes) == 0:

if queue:

queue.put(None)

sys.exit(0)

# Unpack message length as 4 byte integer.

text_length = struct.unpack('i', text_length_bytes)[0]

# Read the text (JSON object) of the message.

text = sys.stdin.read(text_length).decode('utf-8')

if queue:

queue.put(text)

else:

# In headless mode just send an echo message back.

send_message('{"echo": %s}' % text)

if Tkinter:

class NativeMessagingWindow(Tkinter.Frame):

def __init__(self, queue):

self.queue = queue

Tkinter.Frame.__init__(self)

self.pack()

self.text = Tkinter.Text(self)

self.text.grid(row=0, column=0, padx=10, pady=10, columnspan=2)

self.text.config(state=Tkinter.DISABLED, height=10, width=40)

self.messageContent = Tkinter.StringVar()

self.sendEntry = Tkinter.Entry(self, textvariable=self.messageContent)

self.sendEntry.grid(row=1, column=0, padx=10, pady=10)

self.sendButton = Tkinter.Button(self, text="Send", command=self.quiqw)

self.sendButton.grid(row=1, column=1, padx=10, pady=10)

self.quitButton = Tkinter.Button(self, text="Quit",command=quitxy)

self.quitButton.grid(row=1, column=2, padx=10, pady=10)

self.after(100, self.processMessages)

def processMessages(self):

while not self.queue.empty():

message = self.queue.get_nowait()

#if message == None:

#self.quit()

#return

self.log("Received %s" % message)

#main_window.quit()

#mssg=""

#msg=message

#self.log("Received %s" % message)

f = asksaveasfile(mode='w', defaultextension=".txt", initialfile="maple123.txt", initialdir="C:\Users\Prasanth\Desktop")

if not f:

return

f.write(message)

f.close()

self.after(100, self.processMessages)

def quiqw(self):

text = '{"text": "' + self.messageContent.get() + '"}'

self.log('Sending %s' % text)

global flag

if(flag==0):

self.log("DONE FLAG")

try:

send_message(text)

except IOError:

tkMessageBox.showinfo('Native Messaging Example',

'Failed to send message.')

sys.exit(1)

def log(self, message):

self.text.config(state=Tkinter.NORMAL)

self.text.insert(Tkinter.END, message + "\n")

self.text.config(state=Tkinter.DISABLED)

#def Main():

if not Tkinter:

send_message('"Tkinter python module wasn\'t found. Running in headless ' +

'mode. Please consider installing Tkinter."')

read_thread_func(None)

sys.exit(0)

queue = Queue.Queue()

main_window = NativeMessagingWindow(queue)

main_window.master.title('Native Messaging Example')

thread = threading.Thread(target=read_thread_func, args=(queue,))

thread.daemon = True

thread.start()

main_window.mainloop()

sys.exit(0)

#if __name__ == '__main__':

#Main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值