只有一个运行Python程序(如Firefox)? [英] Only one python program running (like Firefox)?

查看:188
本文介绍了只有一个运行Python程序(如Firefox)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开Firefox时,运行命令:

  firefox http:// somewebsite 

在Firefox的新选项卡中打开该URL(同样的事情也发生在Chromium上)。有什么方法可以在Python中复制这种行为?例如,调用:

  processStuff.py文件/ url 

然后调用:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ / code>

不应启动两个不同的进程,而是发送消息到当前正在运行的程序。例如,您可以在一个选项卡式对话框中输入信息,而不是10个单独的窗口。



为任何可以描述 Chromium以跨平台的方式执行此操作。

解决方案

Firefox的做法是:第一个实例创建一个套接字文件或Windows上的命名管道)。这既可以作为Firefox的下一个实例检测并与第一个实例进行通信的方式,并在死亡之前将其转发给URL。套接字文件或命名管道只能从本地系统上运行的进程访问(如同文件一样),网络客户端无法访问它。因为它们是文件,所以防火墙也不会阻塞它们(就像写在文件上一样)。



这是一个天真的实现来说明我的观点。首次启动时,会创建套接字文件 lock.sock

 导入套接字
导入套接字

SOCKET_FILENAME ='lock.sock'

def server():
print'我是服务器,创建套接口'
s = socket .socket(socket.AF_UNIX,socket.SOCK_DGRAM)
s.bind(SOCKET_FILENAME)

尝试:
而真:
print'得到一个URL:%s '%s.recv(65536)
除了KeyboardInterrupt,exc:
print'退出,删除套接字文件'
s.close
os.remove(SOCKET_FILENAME)

def client():
print'我是客户端,打开套接字'
s = socket.socket(socket.AF_UNIX,socket.SOCK_DGRAM)
s .connect(SOCKET_FILENAME)
s.send('http://stackoverflow.com')
s.close()

def main():
if os.path.exists(SOCKET_FILENAME):
try:
client()
除了(socket.error):
打印错误的套接字文件,程序意外关闭?
os.remove(SOCKET_FILENAME)
server()
else:
server()

main()
docs.python.org/library/socketserver.htmlrel =nofollow> SocketServer ,但这是超出这个问题。 Python Socket编程说明也可能对您有所帮助。我没有Windows机器可用,所以我不能确认它在该平台上工作。


When I open Firefox, then run the command:

firefox http://somewebsite

the url opens in a new tab of Firefox (same thing happens with Chromium as well). Is there some way to replicate this behavior in Python? For example, calling:

processStuff.py file/url

then calling:

processStuff.py anotherfile

should not start two different processes, but send a message to the currently running program. For example, you could have info in one tabbed dialog box instead of 10 single windows.

Adding bounty for anyone who can describe how Firefox/Chromium do this in a cross-platform way.

解决方案

The way Firefox does it is: the first instance creates a socket file (or a named pipe on Windows). This serves both as a way for the next instances of Firefox to detect and communicate with the first instance, and forward it the URL before dying. A socket file or named pipe being only accessible from processes running on the local system (as files are), no network client can have access to it. As they are files, firewalls will not block them either (it's like writing on a file).

Here is a naive implementation to illustrate my point. On first launch, the socket file lock.sock is created. Further launches of the script will detect the lock and send the URL to it:

import socket
import os

SOCKET_FILENAME = 'lock.sock'

def server():
    print 'I\'m the server, creating the socket'
    s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    s.bind(SOCKET_FILENAME)

    try:
        while True:
            print 'Got a URL: %s' % s.recv(65536)
    except KeyboardInterrupt, exc:
        print 'Quitting, removing the socket file'
        s.close
        os.remove(SOCKET_FILENAME)

def client():
    print 'I\'m the client, opening the socket'
    s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    s.connect(SOCKET_FILENAME)
    s.send('http://stackoverflow.com')
    s.close()

def main():
    if os.path.exists(SOCKET_FILENAME):
        try:
            client()
        except (socket.error):
            print "Bad socket file, program closed unexpectedly?"
            os.remove(SOCKET_FILENAME)
            server()
    else:
        server()

main()

You should implement a proper protocol (send proper datagrams instead of hardcoding the length for instance), maybe using SocketServer, but this is beyond this question. The Python Socket Programming Howto might also help you. I have no Windows machine available, so I cannot confirm that it works on that platform.

这篇关于只有一个运行Python程序(如Firefox)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆