简体   繁体   English

Python套接字编程

[英]Python Socket Programming

I am developing a testbed for cloud computing environment. 我正在开发用于云计算环境的测试平台。 I want to establish multiple client connection to a server. 我想与服务器建立多个客户端连接。 What I want is that, server first of all send a data to all the clients specifying sending_interval and then all the clients will keep on sending their data with a time gap of that time_interval (as specified by the server). 我想要的是,服务器首先向指定了sending_interval的所有客户端发送数据,然后所有客户端将继续以该time_interval的时间间隔(由服务器指定)发送其数据。 Please help me out, how can I do the same using python socket program. 请帮帮我,如何使用python socket程序做同样的事情。 (ie I want multiple client to single server connectivity and also client sending data with the time gap specified by server). (即,我希望多个客户端到单个服务器连接,也希望客户端在服务器指定的时间间隔内发送数据)。 Will be great-full if anyone can help me. 如果有人可以帮助我,我会感到非常满意。 Thanks in advance. 提前致谢。

This problem is easily solved by the ZeroMQ socket library . ZeroMQ套接字库可以轻松解决此问题。 It is production stable. 生产稳定。 It allows you to define publisher-subscriber relationships, where a publishing process will publish data on a port regardless of how many (0 to infinite) listening processes there are. 它允许您定义发布者与订阅者之间的关系,其中发布过程将在端口上发布数据,而不管有多少个(0到无限)侦听过程。 They call this the PUB-SUB model; 他们称其为PUB-SUB模型; it's in their docs (link below). 在他们的文档中(下面的链接)。

It sounds like you want to set up a bunch of clients that are all publishers. 听起来您想设置一堆都是发布者的客户。 They can subscribe to a controlling channel, which which will send updates to their configuration (how often to write). 他们可以订阅一个控制通道,该通道将向其配置发送更新(写频率)。 They also act as publishers, pushing out their own data at an interval specified by default/config channel/socket. 他们还充当发布者,以默认/配置通道/套接字指定的间隔推出自己的数据。

Then, you have one or more listening processes that listen to all the clients' published messages. 然后,您将具有一个或多个侦听进程,以侦听所有客户端发布的消息。 Perhaps you could even have two listening processes, one for backup or DR, or whatever. 也许您甚至可以有两个侦听过程,一个用于备份或DR,或其他。

We're using ZeroMQ and loving the simplicity it gives ; 我们使用ZeroMQ并喜欢它提供的简单性 ; there's no connection errors because the publisher doesn't care if anyone is listening, and the subscriber can start before the publisher and if there's nothing there to listen to, it can just loop around and wait until there is. 没有连接错误,因为发布者不关心是否有人在听,并且订阅者可以在发布者之前开始,并且如果那里没有可听的内容,它可以循环并等待直到出现。

Bindings are available in ALL languages (it's freaky). 绑定支持所有语言(很奇怪)。 The Python binding isn't pure-python, it does require a C compiler, but is frighteningly fast, and the pub/sub example is a cut/paste, 'golly, it works!' Python绑定不是纯Python,它确实需要C编译器,但速度惊人,而且pub / sub示例是剪切/粘贴操作,“太棒了,它起作用了!” experience. 经验。

Link: http://zeromq.org 链接:http: //zeromq.org

There are MANY other methods available with this library, including message queues, etc. They have relatively complete documentation, too. 该库还有许多其他可用的方法,包括消息队列等。它们也具有相对完整的文档。

Multi-Client and Single server Socket programming can be achieved by Multithreading in Socket Programming. 多客户端和单服务器套接字编程可以通过套接字编程中的多线程来实现。 I have implemented both the method: 我已经实现了两种方法:

  1. Single Client and Single Server 单客户端和单服务器
  2. Multiclient and Single Server 多客户端和单服务器

In my GitHub Repo Link: https://github.com/shauryauppal/Socket-Programming-Python 在我的GitHub Repo链接中: https : //github.com/shauryauppal/Socket-Programming-Python

What is Multi-threading Socket Programming? 什么是多线程套接字编程? Multithreading is a process of executing multiple threads simultaneously in a single process. 多线程是在单个进程中同时执行多个线程的进程。

To understand well you can visit Link: https://www.geeksforgeeks.org/socket-programming-multi-threading-python/ , written by me. 要了解得很好,您可以访问我写的链接: https : //www.geeksforgeeks.org/socket-programming-multi-threading-python/

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

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