为什么我的图像不发送?

时间:2017-02-19 17:44:15

标签: python python-3.x sockets networking send

在我继续之前,请允许我说我在互联网上搜索并在这里寻找答案,但我找不到任何答案。我的问题是我的客户端没有发送我的图像,两者都只是一堆代码。 这是客户端代码:

ok=False
import sys
import socket
import pickle
import os
import time
import getpass
from passlib.hash import pbkdf2_sha256
global x
usern=getpass.getuser()
x=0
password1=False
password2=None
path="C:/Users/"+usern+"/Desktop/pictures/"
curdir=os.getcwd()
print(curdir)
host="192.168.178.62"
port=5002


def Help():                     #help part
    print("Help Menue")
    print("-----------")
    print("")
    print("Add picture:")
    print("Add a picture to the Databank")
    print("The picture should be a picture from a persons Face how can enter without warning")
    print("Every person how is not in the Dantabank will create an alarm message ")
    print("If you use it the first time the programm create a picture folder for all pictures.")
    print("If you want to add a picture put it in this folder and rund Addpicture")
    print("Pleas name the pictures like the person who is on the picture ")
    print("")
    print("-------------------------------------------------------------------")
    print("Delete pciture")
    print("Here you can delete pictures of the Databank so every persons Face you delete will not enter without a alarm message")
    print("")
    print("-------------------------------------------------------------------")
    print("Add mail Adress:")
    print("This option allows you to add a mail adress were the alarm message is going to you can add so many as you like but max 50")
    print("")
    print("-------------------------------------------------------------------")
    print("For Help:")
    print("Open this Help menue")
    print("")
    print("-------------------------------------------------------------------")
    print("Logout:")
    print("That I dont explain")
    t=input("Press a button to exit: ")
    if t=="":
        print("")
        print("")
        main()


def delpic():                       #the function for delete a picture
    s=socket.socket()
    try:
        s.connect((host, port))
    except socket.error:
        print("")
        print("Could not connect to the server")
        print("")
        main()
    s.send(str.encode("Del"))
    direList=s.recv(2400)
    print("Here are the picture names:" + direList.decode("utf-8"))
    if direList=="Nothing in this Directory!":
        print("")
        main()
    delete=input("Wich picture do you want to delete: ")
    if delete=="q":
        print("")
        main()
    s.send(str.encode("delete"))
    ok=s.recv(1024)
    if ok=="True":
        print("Picture is Deleted")
        o=input("Do you want to continue? [y/n]")
        if o=="y":
            delpic()
        else:
            main()
    else:
        print("Something goes wrong")
        print("")
        main()






def addpic():           #the add Picture function
    global host
    global port
    print("Enter the name of the picture")
    print(" ")
    try:
        t=os.chdir(path)    #test if the dir exist
    except OSError:
        print("error")
        t=False
    if t==False:
        os.chdir("C:/Users/"+usern+"/Desktop")
        os.mkdir("pictures")
        print(os.getcwd())
    print(os.getcwd())
    picname=input("Enter the picture name: ")
    try:
        os.chdir(path)
        os.path.isfile(picname)
    except( FileExistsError, FileNotFoundError):
        print("The file could not found try it aggain")
        q=input("Do you want to continue? [y/n]")
        if q=="y":
            addpic()
        if q=="n":
            print("")
            main()

    print("Picture found, Data will send pls wait...")
    s=socket.socket()
    s.connect((host,port))
    try:
        s.send(str.encode("picture"))
        s.send(str.encode(picname))
        with open(picname,"rb") as f:
            chunk=f.read(1024)
            while chunk:
                s.send(chunk)
                chunk=f.read(1024)
                if not chunk:
                    f.close()
    except socket.error:
        print("Could not connact to the server. Please check your Connection!")
    back=s.recv(1024)
    back=back.encode("utf-8")
    if back=="ok":
        print("Image has been sucsessfully sended!")
    else:
        print("image was not sended!")
        print("Check your Connection")
    m=input("Do you want to continue? [y/n]")
    if m=="y":
        addpic()
    if m=="n":
        print("")
        print("")
        main()




def main():         #main part with options
    global user
    global passw
    print("1:Add picture")
    print("2:Delete picture")
    print("3:Add mail Adress")
    print("4:For help")
    print("5:For logout")
    wish=int(input("What do you want to do?: "))
    if wish==1:
        addpic()
    if wish==2:
        delpic()
    if wish==3:
        addmail()
    if wish==4:
        Help()
    if wish==5:
        time.sleep(1)
        sys.exit()
        exit()
    if (KeyboardInterrupt, ValueError, Exception):
        print("")
        main()



def create():                           #the funktion were the user add his information
    global password1
    global password2
    global hash
    global host
    global port
    Pass_ok=False   #add all important infos
    user=input("Create a Username: ")
    print("Welcome "+user)
    username=open("username.pkl", "wb")
    save=pickle.dump(user,username)
    while password1!=password2:
        password1=input("Enter your Password: ")
        password2=input("Confirm your Password: ")
        Pass_ok=True

    if Pass_ok:
        hash = pbkdf2_sha256.encrypt(password2, rounds=200000, salt_size=16)
        password=open("pass.pkl","wb")
        save2=pickle.dump(hash, password)


    mail=input("Enter your Mail adress:")   #more important infos
    key=input("Enter your Instapush appid: ")
    secrect=input("Enter your Instapush secret: ")
    password=open("pass.pkl","wb")
    mail2=open("mail.txt","wb")
    key2=open("Key.pkl","wb")
    save3=pickle.dump(mail,mail2)
    username.close()
    password.close()
    s=socket.socket()
    s.connect((host,port))
    mail=open("mail.txt","rb")
    chunk=mail.read(1024)
    s.send(str.encode("mail"))
    while chunk:                    #this funktions should send the Mail.txt content
        s.send(chunk)
        chunk=mail.read(1024)
        if not chunk():
            mail.close()
            break

    print("")
    main()




def login():    #is the login menue
    global x
    global hash
    while x!=3:
        Pass=input("Enter your Password: ")
        right=pbkdf2_sha256.verify(Pass, hash)
        if right==True:
            print("")
            print("Welcome back "+user)
            print("")
            main()

            if right==False:
                x=x+1

            if x==3:
                print("That goes Wrong...")
                exit()
                sys.exit()

try:
    hash=pickle.load(open("pass.pkl","rb"))               #this is the part were everything Load dont know if that is working
    user=pickle.load(open("username.pkl","rb"))
    mail=pickle.load(open("mail.txt","rb"))
    ok=True
except(FileExistsError, FileNotFoundError, EOFError):
    print("Loading Error")
    create()



if ok==True:    #check if all is working
    login()

else:
    print("Load error") # this is the error if the file cant Load
    exit()
    sys.exit()

这是服务器代码:

import os
import sys
import pickle
import socket
import time
mail_path="/home/pi/Desktop/Pi_Secure/mail"
picture_path="/home/pi/Desktop/Pi_Secure/picture"
Video_path="/home/pi/Desktop/Pi_Secure/video"
key_path="/home/pi/Desktop/Pi_Secure/key"
main_path="/home/pi/Pi_Secure"
host="192.168.178.45"
port= 5001
s=socket.socket()
s.bind((host, port))



print("Server is online at "+host+":"+str(port))

def get_data():
    global host
    global port
    global picture_path
    global Video_path
    global key_path
    name=None
    s.listen(1)
    c ,addr=s.accept()
    print("Connection established to"+str(addr))
    data=c.recv(1024)
    data=data.decode("utf-8")
    dataMessage=data.split(" ", 1)  #split data in command a picture or text Dokument
    command=dataMessage[0]

    if command=="mail":
        os.chdir(mail_path)
        mail=open(mail_path,"wb")
        Mail=c.recv(1024)
        while Mail:
            mail.write(Mail)
            Mail=c.recv(1024)
            if not mail:
                mail.close()
                break





    if command=="key":
        os.chdir(key_path)
        key=open(key_path,"wb")
        Key=c.recv(1024)
        while Key:
            key.write(Key)
            Key=c.recv(1024)
            if not key:
                key.close()
                break


    if command=="picture":
        os.chdir(picture_path)
        picture=open(picture_path,"wb")
        pic=c.recv(1024)
        while pic:
            picture.write(pic)
            pic=c.recv(1024)
            if not pic:
                c.send("ok")
                picture.close()
                break



    if command=="Del":
        try:
            t=os.listdir()
        except OSError:
            t="Nothing is in this Diretory"
            o=t,encode("utf-8")
            c.send(o)
            c.close()
        o=t.encode("utf-8")
        c.send(o)
        pickname=c.recv(1024)
        Picture=pickname.decode("utf-8")
        os.remove(Picture)
        c.send(str.encode("True"))

while True:
    if os.path.exists(main_path):      #this should check that everything what is important be existent
        get_data()

if socket.error:
    get_data()

else:
    os.chdir("/home/pi/Desktop")
    os.mkdir("Pi_Secure")
    os.chdir("/home/pi/Desktop/Pi_Secure/")
    os.mkdir("pictures")
    os.mkdir("mail")
    os.mkdir("key")
    get_data()


global c,addr
c.close()

0 个答案:

没有答案