简体   繁体   English

在python中创建两台计算机之间的连接

[英]Creating connection between two computers in python

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? 问题:如何创建一个python应用程序,可以通过Internet连接和发送数据包到运行相同应用程序的另一台计算机? Is there any existing code/library I could use? 我可以使用现有的代码/库吗?

The background: I am pretty new to programming (HS senior). 背景:我对编程很新(HS高级)。 I've created a lot of simple things in python but I've recently decided to start on a bigger project. 我在python中创建了很多简单的东西,但我最近决定开始一个更大的项目。 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! 谢谢!

Twisted is a python event-driven networking engine licensed under MIT. Twisted是一个在MIT下获得许可的python事件驱动的网络引擎。 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 (ie using HTTP for communication means middleware software that uses HTTP will be compatible: proxies etc.) 这是更好的,因为你从第三方软件获得对协议的支持(即使用HTTP进行通信意味着使用HTTP的中间件软件将兼容:代理等)

It also makes easy to create your own communication protocol, if that's what you want. 如果这就是你想要的,它也可以很容易地创建自己的通信协议。

The documentation is filled with examples. 文档中充满了示例。

The standard library includes SocketServer (documented here ), which might do what you want. 标准库包括SocketServer( 此处记录 ),可能会执行您想要的操作。

However I wonder if a better solution might be to use a message queue. 但是我想知道更好的解决方案是否可以使用消息队列。 Lots of good implementations already exist, including Python interfaces. 已经存在许多好的实现,包括Python接口。 I've used RabbitMQ before. 我以前用过RabbitMQ The idea is that the computers both subscribe to the queue, and can either post or listen for events. 这个想法是计算机都订阅队列,并可以发布或监听事件。

A great place to start looking is the Python Standard Library . 一个开始寻找的好地方是Python标准库 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. 既然你提到你没有这方面的经验,我建议从基于HTTP的实现开始。 The HTTP protocol is fairly simple and easy to work with. HTTP协议非常简单易用。 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. 此外,也有很好的框架,以支持此操作,如webpy服务器和httplib的来自客户端的标准库。

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. 这将教会您在许多网络代码中使用的基础概念,同时产生类似于文件流的接口。

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). 这意味着您可能希望从使用TCP连接的SocketServer开始(TCP基本上保证数据传输; UDP不是)。

Google for some simple example code. Google提供了一些简单的示例代码。 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. 但是你必须定义你的通信协议的所有细节:哪一端发送什么时候,什么,哪一端听,什么时候,听众会期待什么,回复确认收据等等。

Also, check out Pyro (Python remoting objects). 另外,查看Pyro (Python远程对象)。 Se this answer for more details. 有关详细信息,请参阅此答案

Pyro basically allows you to publish Python object instances as services that can be called remotely. Pyro基本上允许您将Python对象实例发布为可以远程调用的服务。 Pyro is probably the easiest way to implement Python-to-python process communication. Pyro可能是实现Python-to-python进程通信的最简单方法。

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. 同样值得看看Kamaelia这种事情 - 它的原始用例是网络系统,并且使构建这样的东西相对直观。 Some links: Overview of basic TCP system , Simple Chat server , Building a layered protocol , walk-through of how to evolve new components . 一些链接: 基本TCP系统概述简单聊天服务器构建分层协议如何发展新组件的演练 Other extreme: P2P radio system: source , peer . 其他极端:P2P无线电系统: 同行

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. 如果它有所不同,我们已经通过参与谷歌夏季代码连续3年测试了系统是否可供初学者访问,积极地吸引有经验和缺乏经验的开发人员。 All of them managed to build useful systems. 所有这些人都设法建立了有用的系统。

Essentially, if you've ever played with unix pipelines the ideas should be familiar. 从本质上讲,如果您曾经使用过unix管道,那么这些想法应该是熟悉的。

Caveat: I wrote major chunks of Kamaelia :-) 警告:我写了大量的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. 如果你想学习如何做这些事情,玩几种不同的方法是有道理的,你一定要看看Twisted(这个问题的标准答案),Pyro和标准库工具。 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. 但是,像nosklo一样,我建议不要直接使用套接字库而是使用库 - 只是因为让套接字编程比人们容易意识到的要难得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM