简体   繁体   English

相同的python脚本,但结果不同

[英]same python script, but different results

Hello stackoverflow community, I wrote a simple socket based Python script and executed it ~20 times. 您好stackoverflow社区,我编写了一个基于套接字的简单Python脚本,并执行了约20次。 18 times it did do what it was supposed to do, but 2 times it printed some randome Letters. 它完成了18倍的预期工作,但打印了2次随机字母。

(It was supposed to send and print every foldername in a certain directory) (应该发送并打印某个目录中的每个文件夹名称)

Server: 服务器:

import socket
import os
import base64

host = ''
port = 6934

s = socket.socket()

s.bind((host, port))

s.listen(5)

while True:
    c, addr = s.accept()

    req = c.recv(512)

    print "%s has connected w REQ %s" %(str(addr), str(req))

    amount = len(os.listdir("./software"))

    c.send(str(amount))
    print amount

    for dir in os.listdir("./software"):
        name = str(dir)
        c.send()
        with open("./software/"+dir+"/img.jpg", "rb") as image:
            image.seek(0)
            imageStr = image.read().encode("base64")
            c.send(imageStr)
            print "imageStr"
        with open("./software/"+dir+"/text.txt", "r+") as desc:
            desc.seek(0)
            descStr = desc.read()
            c.send(descStr)
            print descStr
    c.close()
s.close()

Client: 客户:

import socket

class Connection(object):
    def __init__(self, name, desc):
        self.name = name
        self.desc = desc

objList = []

def fetcher(request):

    host = "192.168.178.87"
    port = 6934

    s = socket.socket()

    s.connect((host, port))

    s.send(request)

    amount  = int(s.recv(128))

    for i in range(0, amount):
        name = s.recv(512)
        content = s.recv(32768)
        desc = s.recv(8192)
        objList.append(Connection(name, desc))

def window():
    for i  in objList:
        print i.name
        print i.desc
    print "Window loop"
def main(request):
    fetcher(request)
    window()
main("greetings")

The Console Output (I didn't change anything in the Code) Server: 控制台输出(我未在代码中进行任何更改)服务器:

('192.168.178.87', 56752) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56754) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56756) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56757) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56758) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56759) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56761) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56762) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56763) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56764) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56765) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56766) has connected w REQ greetings
1
imageStr
Hallo
('192.168.178.87', 56767) has connected w REQ greetings
1
imageStr
Hallo

The Console Output (I didn't change anything in the Code) Client:(The reason I am here ...) 控制台输出(我未在代码中进行任何更改)客户端:(我在这里的原因...)

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
oneQk12AQAAAAAAADYAAAAoAAAACgAAAAoAAAABABgAAAAAAEABAAAAAAAAAAAAAAAAAAAAAAAAJBzt
JBztJBztJBztJBztJBztJBztJBztJBztJBztAAAkHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0k
HO0AACQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7QAAJBztJBztJBztJBztJBztJBztJBzt
JBztJBztJBztAAAkHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0AACQc7SQc7SQc7SQc7SQc
7SQc7SQc7SQc7SQc7SQc7QAAJBztJBztJBztJBztJBztJBztJBztJBztJBztJBztAAAkHO0kHO0k
HO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0AACQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7QAA
JBztJBztJBztJBztJBztJBztJBztJBztJBztJBztAAA=


Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one

Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
oneQk12AQAAAAAAADYAAAAoAAAACgAAAAoAAAABABgAAAAAAEABAAAAAAAAAAAAAAAAAAAAAAAAJBzt
JBztJBztJBztJBztJBztJBztJBztJBztJBztAAAkHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0k
HO0AACQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7QAAJBztJBztJBztJBztJBztJBztJBzt
JBztJBztJBztAAAkHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0AACQc7SQc7SQc7SQc7SQc
7SQc7SQc7SQc7SQc7SQc7QAAJBztJBztJBztJBztJBztJBztJBztJBztJBztJBztAAAkHO0kHO0k
HO0kHO0kHO0kHO0kHO0kHO0kHO0kHO0AACQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7SQc7QAA
JBztJBztJBztJBztJBztJBztJBztJBztJBztJBztAAA=


Window loop

C:\Users\LeEmpollon\Desktop\Cream>python cream.py
one
Hallo
Window loop

I am using Windows 7 Professional 64bit, Python 2.7,both scripts were executed on the same computer, and the scripts and folders are in the same directory : 我正在使用Windows 7 Professional 64位,Python 2.7,两个脚本都在同一台计算机上执行,并且这些脚本和文件夹位于同一目录中:

folder
-software
--one
---img.jpg (10x10px)
---text.txt(contains the word "Hello")
--creamserver.py
--cream.py

Thanks for reading :) 谢谢阅读 :)

I think you're misunderstanding how sockets work. 我认为您误会了套接字的工作方式。 The socket doesn't keep track of any kind of "chunks" of data that are sent as part of a single send , or received with a single recv . 套接字无法跟踪作为单个send一部分send或通过单个recv接收的任何类型的数据“块”。 The socket is like a pipe where you stuff data in one end and pull it out the other end. 套接字就像一个管道,在其中将数据填充到一端,然后将数据拉出另一端。

If you send name , imageStr , and desc , that's the same as sending all three of them together in one big blob. 如果发送nameimageStrdesc ,则与将它们全部三个一起发送到一个大Blob中相同。 At the other end, your client code has no way to tell where name ends and imageStr begins. 另一方面,您的客户端代码无法分辨name在何处结束,而imageStrimageStr开始。

The intermittent nature of the behavior is due to the unpredictable timing of the independent operation of the client and server. 行为的间歇性是由于客户端和服务器的独立操作的时间不可预测。 If your server manages to send both the name and the imageStr before the client does its recv , then the client will receive both at once. 如果您的服务器在客户端执行recv之前设法同时发送了nameimageStr ,则客户端将一次接收到两者。 If the client happens to do a recv in between your sending the two pieces of data, then the recv will only receive whatever was sent by that point. 如果客户端恰巧在您发送两段数据之间进行一次recv ,那么recv将仅接收到该点发送的任何内容。 In your case, it looks like the former happened twice and the latter happened once. 在您的情况下,前者发生了两次,而后者发生了一次。

These issues are explained in the Socket Programming HOWTO , which you should read. 这些问题将在Socket Programming HOWTO中进行解释,您应该阅读。 Especially this part: 特别是这部分:

Now if you think about that a bit, you'll come to realize a fundamental truth of sockets: messages must either be fixed length (yuck), or be delimited (shrug), or indicate how long they are (much better), or end by shutting down the connection . 现在,如果您考虑一下,您将认识到套接字的基本原理: 消息必须是固定长度的 (yuck)或定界的 (耸耸肩),或者指出它们的长度 (好得多),或者通过关闭连接结束

Since your messages don't do any of those things, you can't reliably use the socket at all. 由于您的消息不执行任何这些操作,因此根本无法可靠地使用套接字。

The socket doesn't do anything for you. 套接字对您没有任何帮助。 It doesn't delimit, chunk, group, or structure your data in any way. 它不会以任何方式对数据进行定界,分块,分组或结构化。 It just shoves bytes to the other end. 它只是将字节推到另一端。 If you want to do things like send separate pieces of data and then recapture them as separate pieces, you have to handle that all yourself (by delimiting or length-marking your data as suggested in the docs). 如果您想做一些事情,例如发送单独的数据,然后将它们重新捕获为单独的数据,则您必须自己处理所有事情(按照文档中的建议对数据进行定界或加长标记)。 Or, alternatively, use a higher-level network protocol of some sort rather than working directly with sockets, because due to these limitations, working directly with sockets is often a pain in the butt. 或者,或者选择使用某种更高级别的网络协议,而不是直接使用套接字,因为由于这些限制,直接使用套接字通常会带来麻烦。

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

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