简体   繁体   中英

same python script, but different results

Hello stackoverflow community, I wrote a simple socket based Python script and executed it ~20 times. 18 times it did do what it was supposed to do, but 2 times it printed some randome Letters.

(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 :

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 . 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. At the other end, your client code has no way to tell where name ends and imageStr begins.

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. 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. 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. 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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