python udp 大文件_Python UDP服务器发送文本文件的行(Python UDP Server send lines of a text file)...

Python UDP服务器发送文本文件的行(Python UDP Server send lines of a text file)

我需要模拟一个UDP服务器,它在无限循环中逐行发送文本文件的内容。 我已经编写了下面的代码,但在另一端我没有收到任何东西(另一方面是Qt代码,我相信它有效):

import socket

import time

# setup UDP socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sa = ('localhost', 9763)

sock.bind(sa)

filename = '/home/saeid/Documents/data.txt' # file to read

numlines = sum(1 for line in open(filename)) # get number of lines in file

# get line of a file

def get_line(line):

with open(filename) as fp:

for i, l in enumerate(fp):

if i == line:

return l

# main loop

while True:

currline = 0

while currline < numlines:

sock.sendto(get_line(currline), sa)

currline += 1

time.sleep(1)

我不是蟒蛇亲和不能找出问题:(

I need to simulate a UDP server, which sends content of a text file line by line in an endless loop. I have written the code below but at the other end I do not receive anything (The other side is Qt code and I am sure it works):

import socket

import time

# setup UDP socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sa = ('localhost', 9763)

sock.bind(sa)

filename = '/home/saeid/Documents/data.txt' # file to read

numlines = sum(1 for line in open(filename)) # get number of lines in file

# get line of a file

def get_line(line):

with open(filename) as fp:

for i, l in enumerate(fp):

if i == line:

return l

# main loop

while True:

currline = 0

while currline < numlines:

sock.sendto(get_line(currline), sa)

currline += 1

time.sleep(1)

I am no python pro and cant figure out the problem :(

原文:https://stackoverflow.com/questions/40365448

2020-03-02 11:15

满意答案

对于初学者来说这很混乱:

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sa = ('localhost', 9763)

sock.bind(sa)

...

sock.sendto(get_line(currline), sa)

通过绑定你基本上说“我想听那个主机/端口”。 但是,然后您将数据发送到相同的主机/端口。 我假设有一些其他的目标地址,例如sock.sendto(get_line(currline), ('my_host', 1234)) 。 顺便说一句,你为什么要绑定到地址呢? sock.bind(sa)行是不必要的,删除它。

另一件事是,你的文件阅读代码是非常低效和难以阅读的(我花了一段时间才明白发生了什么)。 尝试这样的事情:

with open(filename, 'r') as fo:

while True:

for line in fo:

sock.sendto(line, DEST_ADDRESS)

time.sleep(1)

fo.seek(0) # go back to the begining of the file and repeat

并摆脱get_line函数。

这至少是我阅读你的描述后想到的。 如果你不想永远发送相同的文件,那么你可以摆脱while True: loop和fo.seek(0)调用。

For starters this is messed up:

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sa = ('localhost', 9763)

sock.bind(sa)

...

sock.sendto(get_line(currline), sa)

By binding you basically say "I want to listen on that host/port". But then you send the data to the same host/port. I assume that there's some other destination address, e.g. sock.sendto(get_line(currline), ('my_host', 1234)). By the way, why are you binding to the address anyway? sock.bind(sa) line is unnecessary, remove it.

The other thing is that your file reading code is extremely inefficient and difficult to read (it took me a while to understand what's going on). Try something like this:

with open(filename, 'r') as fo:

while True:

for line in fo:

sock.sendto(line, DEST_ADDRESS)

time.sleep(1)

fo.seek(0) # go back to the begining of the file and repeat

and get rid of get_line function.

That's at least what I've came up with after reading your description. If you don't want to inifinitely send the same file then you can get rid of while True: loop and fo.seek(0) call.

2016-11-01

相关问答

您需要服务器B的本地端点,以及每个客户端A的服务器C的远程端点。 这是一个可能的实现 。 此外,您可能对此模块感兴趣, 该模块为asyncio提供高级UDP端点。 You need a local endpoint for server B, plus a remote endpoint to server C for each client A. Here is a possible implementation. Also, you might be interested in this mo...

没有连接到服务器的情况下,您的计算机无法发送邮件(否则邮件将如何从您的计算机中卸载?)。 大多数人都有一个随时可用的SMTP服务器,可以由他们的公司(如果这是在内部网上)或由他们的ISP(如果是家庭用户)提供给他们。 你需要主机名(通常是smtp1.myispdomain.com,当然myispdomain是你的其他东西)和一个端口号(通常是25)。有时主机被提供为一个数字IP地址,如192.168.0.1。 SMTP()调用可以接受这些参数并自动连接到服务器。 如果在创建SMTP对象时不提供参数...

ready_client1 = select.select([sock_client1], [], [], None)

ready_client2 = select.select([sock_client2], [], [], None)

尝试使用单个select : ready_read, ready_write, exceptional = select.select(

[sock_client1, sock_client2], [], [], None)

for ready i...

来自client.c: int nbrecv = 0;

while(flag == 1){

memset(buffer, '\0', BUFFER_SIZE);

if((nbrecv = recvfrom(s, buffer, BUFFER_SIZE, 0, &remote, &len_remote)) == -1){

fprintf(stderr, "fail while receiving data! \n");

exit(-1)...

您可以像这样创建基于JSON的“protocole”: {“items”:[{“type”:'string“,”value“:”test“},{”type“:”int“,”value“:3}]} 并使用https://code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-1_-_Encode_a_JSON_object对其进行编码。 You can create "protocole" JSON-based like this:...

不要使用UDP传输大文件,请使用TCP。 UDP不会让您发送的所有数据包都到达,或者如果它们按顺序到达,它们甚至可能被复制。 此外,UDP不适合大型传输,因为1)它没有拥塞控制,所以你只会泛滥网络,数据包将被丢弃,2)你必须将你的数据包分解成较小的数据包通常大约1400字节是建议保持在MTU以下,否则如果您依赖IP碎片并且丢失了一个片段,则整个文件都将丢失。您必须编写自定义代码来修复UDP的所有这些问题,因为文件传输需要一切都可靠地发送。 另一方面,TCP已经完成了所有这些,它是可靠的,具有拥塞控...

send不保证将发送所有字节。 它的返回值告诉你你发出了多少请求(我几乎可以保证它不会全部是65KB!)。 您应该继续前进,直到发送所有传出缓冲区。 阅读您使用的功能的文档。 send does not guarantee that all bytes will be sent. Its return value tells you how much was sent out of what you requested (and I can pretty much guarantee that i...

由于并非所有数据包都显示在wireshark中,我猜测发送方在操作系统中的网络数据包缓冲区空间不足。 UDP不仅会因传输失败而丢失数据包,而且如果任何路由组件的容量耗尽,并且因为太忙而被迫丢弃数据包。 这包括本地互联网协议栈。 首先,检查sendto()中的错误代码。 如果要责怪本地操作系统,则可能会有礼貌地报告错误。 更新:sendto()没有错误,那么,没有简单的解决方法。 最后一点注意事项/建议。 即使主机之间的直接以太网连接也不能保证数据包始终能够通过。 如果您依赖于可靠传输的文件数据,那...

对于初学者来说这很混乱: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sa = ('localhost', 9763)

sock.bind(sa)

...

sock.sendto(get_line(currline), sa)

通过绑定你基本上说“我想听那个主机/端口”。 但是,然后您将数据发送到相同的主机/端口。 我假设有一些其他的目标地址,例如sock.sendto(get_line(currline), ('my_host...

此代码的问题是服务器回复自身,而不是远程客户端。 在这里: data, address = self.sock.recvfrom(8192)

# ...

bytes_sent = self.sock.sendto(msg_server.SerializeToString(), self.remote)

它应该是: bytes_sent = self.sock.sendto(msg_server.SerializeToString(), address)

将remote重命名为server_ad...

相关文章

一、摘要   总结基于C#的UDP协议的同步通信。 二、实验平台   Visual Studio

...

各位大虾晚上好,我有个问题想请教你们,我想美化html的file外观,但貌似现在还不能用css直接设计

...

abs(x) 说明:abs(x)返回x的绝对值,如果参数是复数,则返回复数的模; 参数x:整

...

Data Week: Becoming a data scientist Data Pointed,

...

erver-Sent Events - 单向的信息处理.一个SSE(server send event

...

Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重

...

Java 流(Stream)、文件(File)和IO Java.io包几乎包含了所有操作输入、输

...

python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速

...

最近开发微信接口,配合开发弄了这个微信服务端的模拟以方便调试。 #-*-cod

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值