在python中创建两台计算机之间的连接
问题:如何创建一个 python 应用程序,它可以通过互联网连接并发送数据包到另一台运行相同应用程序的计算机? 有没有我可以使用的现有代码/库?
背景:我对编程还很陌生(高中四年级)。 我已经用 python 创建了很多简单的东西,但我最近决定开始一个更大的项目。 我正在考虑创建一个《Magic: the Gathering》助推器选秀模拟器,但我不确定考虑到我的技能是否可行,所以我在开始之前四处询问。 该应用程序需要在计算机之间发送有关正在拾取/传递卡片的数据。
谢谢!
The question: How do I create a python application that can connect and send packets over the internet to another computer running the same application? Is there any existing code/library I could use?
The background: I am pretty new to programming (HS senior). I've created a lot of simple things in python but I've recently decided to start on a bigger project. I'm considering creating a Magic: the Gathering booster draft simulator, but I'm not sure if it is feasible given my skill set so I'm asking around before I get started. The application would need to send data between computers about which cards are being picked/passed.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Twisted 是一个获得 MIT 许可的 Python 事件驱动网络引擎。 意味着一台机器可以与一台或多台其他机器通信,同时在接收和发送数据之间执行其他操作,所有这些都是异步的,并且在单个线程/进程中运行。
它支持许多开箱即用的协议,因此您也可以使用现有的协议。 这更好,因为您可以从第 3 方软件获得对该协议的支持(即使用 HTTP 进行通信意味着使用 HTTP 的中间件软件将兼容:代理等)。
如果您想要的话,它还可以轻松创建您自己的通信协议。
文档充满了示例。
Twisted is a python event-driven networking engine licensed under MIT. Means that a single machine can communicate with one or more other machines, while doing other things between data being received and sent, all asynchronously, and running a in a single thread/process.
It supports many protocols out of the box, so you can just as well using an existing one. That's better because you get support for the protocol from 3rd party software (i.e. using HTTP for communication means middleware software that uses HTTP will be compatible: proxies etc.)
It also makes easy to create your own communication protocol, if that's what you want.
The documentation is filled with examples.
标准库包括 SocketServer(此处记录),它可能会执行您想要的操作。
但是我想知道是否更好的解决方案是使用消息队列。 许多好的实现已经存在,包括 Python 接口。 我之前使用过 RabbitMQ 。 这个想法是,计算机都订阅队列,并且可以发布或侦听事件。
The standard library includes SocketServer (documented here), which might do what you want.
However I wonder if a better solution might be to use a message queue. Lots of good implementations already exist, including Python interfaces. I've used RabbitMQ before. The idea is that the computers both subscribe to the queue, and can either post or listen for events.
Python 标准库是一个很好的起点。 特别是有几个相关部分:
既然您提到您没有这方面的经验,我建议您从基于 HTTP 的实现开始。 HTTP 协议相当简单且易于使用。 此外,还有一些不错的框架支持此操作,例如用于服务器的 webpy 和 HTTPLib 来自客户端的标准库。
如果您确实想深入研究网络,那么基于 socket 的实现可能具有教育意义。 这将教您大量网络代码中使用的基本概念,同时产生类似于文件流的界面。
A great place to start looking is the Python Standard Library. In particular there are a few sections that will be relevant:
Since you mentioned that you have no experience with this, I'd suggest starting with a HTTP based implementation. The HTTP protocol is fairly simple and easy to work with. In addition, there are nice frameworks to support this operation, such as webpy for the server and HTTPLib from the standard library for the client.
If you really want to dive into networking, then a socket based implementation might be educational. This will teach you the underlying concepts that are used in lots of networking code while resulting in an interface that is similar to a file stream.
另外,请查看 Pyro (Python 远程处理对象)。 有关更多详细信息,请参阅此答案。
Pyro 基本上允许您将 Python 对象实例发布为可以远程调用的服务。 Pyro 可能是实现 Python 到 Python 进程通信的最简单方法。
Also, check out Pyro (Python remoting objects). Se this answer for more details.
Pyro basically allows you to publish Python object instances as services that can be called remotely. Pyro is probably the easiest way to implement Python-to-python process communication.
对于此类事情,Kamaelia 也值得一看 - 它最初的用例是网络系统,并且使得构建这样的事情比较直观。
一些链接:基本 TCP 系统概述、简单聊天服务器,构建分层协议,演练如何发展新组件。 另一个极端:P2P 无线电系统: 来源,同行。
如果有什么区别的话,我们已经通过连续三年参与谷歌代码之夏测试了该系统是否适合新手使用,并积极与经验丰富和缺乏经验的开发人员合作。 他们所有人都设法构建了有用的系统。
本质上,如果您曾经使用过 UNIX 管道,那么这些想法应该很熟悉。
警告:我写了 Kamaelia 的主要部分:-)
如果你想学习如何做这些事情,尝试几种不同的方法是有意义的,你一定要看看 Twisted (这个问题的标准答案)、Pyro & ; 标准库工具。 每个人都有不同的方法,学习它们一定会让你受益匪浅!
然而,像 nosklo 一样,我建议不要直接使用套接字库,而应使用库来代替 - 只是因为正确地进行套接字编程比人们意识到的要困难得多。
It's also worth looking at Kamaelia for this sort of thing - it's original usecase was network systems, and makes building such things relatively intuitive.
Some links: Overview of basic TCP system, Simple Chat server, Building a layered protocol, walk-through of how to evolve new components. Other extreme: P2P radio system: source, peer.
If it makes any difference, we've tested whether the system is accessible to novices through involvement in google summer of code 3 years in a row, actively taking on both experienced and inexperienced developers. All of them managed to build useful systems.
Essentially, if you've ever played with unix pipelines the ideas should be familiar.
Caveat: I wrote major chunks of Kamaelia :-)
If you want to learn how to do these things though, playing with a few different approaches makes sense, and you should definitely check out Twisted (the standard answer to this question), Pyro & the standard library tools. Each has a different approach, and learning them will definitely benefit you!
However, like nosklo, I would recommend against using the socket library directly and use a library instead - simply because it is much much harder to get sockets programming correct than people tend to realise.
通信将通过套接字以一种或另一种方式进行。 只是一个问题,你是否使用现有的更高级别的库,或者推出自己的库。
如果您将此作为一种学习体验,可能希望尽可能从低级别开始,以了解真正的具体细节。 这意味着您可能想从 一个 SocketServer 开始,使用 TCP 连接(TCP 是UDP 基本上不能保证数据传输)。
谷歌一些简单的示例代码。 设置非常简单。 但是您必须定义通信协议的所有详细信息:哪一端何时发送、发送什么内容、哪一端侦听以及何时侦听、侦听器到底期望什么、它是否回复确认接收等。
Communication will take place with sockets, one way or another. Just a question of whether you use existing higher-level libraries, or roll your own.
If you're doing this as a learning experience, probably want to start as low-level as you can, to see the real nuts and bolts. Which means you probably want to start with a SocketServer, using a TCP connection (TCP is basically guaranteed delivery of data; UDP is not).
Google for some simple example code. Setting one up is very easy. But you will have to define all the details of your communications protocol: which end sends when and what, which end listens and when, what exactly the listener will expect, does it reply to confirm receipt, etc.