پروژه ارسالی - اسکریپت ارسال و دریافت فایل در شبکه به صورت گرافیکی :: Mr Python | مستر پایتون

پروژه ارسالی - اسکریپت ارسال و دریافت فایل در شبکه به صورت گرافیکی

  • ۳۸۶
فرستنده Ali Zabetpoor
نام پروژه  اسکریپت ارسال و دریافت فایل در شبکه به صورت گرافیکی
دسته بندی : python
توضیحات : پروژه شامل دو اسکریپت است . 1 - اسکریپت فرستنده 2 - اسکریپت گیرنده . اسکریپت فرستنده فایل مورد نظر رو در شبکه ارسال میکنه و اسکریپت گیرنده اونو دریافت میکنه و ذخیره میکنه . از این اسکریپت ها میتوان برای انتقال فایل در شبکه بر روی کامپیوتر های مختلف استفاده کرد . هر دو اسکریپت دارای رابط گرافیکی هستند .

 

نکته : مستر پایتون هیچ تغییری در سورس کد های ارسالی ایجاد نخواهد کرد و سورس کد مستقیما نوشته خود شخص فرستنده است . 

 

توضیحات MrPython در مورد پروژه :در این پروژه برای طراحی رابط کاربری گرافیکی از کتابخونه ی pySimpleGUI و برای ارتباط شبکه و ارسال و دریافت اطلاعات از کتابخونه ی socket استفاده شده . 

 

برای اجرای صحیح اسکریپت باید کتابخونه ی pySimpleGUI رو نصب کنید . 

pip install pySimpleGUI

 

سورس فرستنده : 

from threading import Thread
import PySimpleGUI as sg
from PySimpleGUI.PySimpleGUI import WINDOW_CLOSED

from socket import AF_INET, SOCK_STREAM, socket

layout=[
        [sg.Text("file location: "),sg.Input(key="-FILE-",size=(30,2)),sg.FileBrowse(size=(20,1))],
        [sg.Text("ip reciever:"),sg.InputText(key="-IP-")],
        [sg.Text("port reciever:"),sg.InputText(key="-PORT-")],
        [sg.Button("send",key="send-btn",size=(8,2))],
        [sg.ProgressBar(2,orientation="h",size=(20,15),key="-progress-bar-",bar_color=("green","white"))],
]

window=sg.Window("sender app",layout=layout,size=(500,200),font="Arial 12",element_justification="c")

def send_file(file,ip,port):
    with open(file=file,mode="rb") as file:
        data=file.read()
    try:
        sock=socket(AF_INET,SOCK_STREAM)
        sock.connect((ip,port))

        bar=int(len(data)/1024)
        i=0
        window["-progress-bar-"].UpdateBar(0,bar)
        while True:
            if len(data)>0:
                tmp_data=data[0:1024]
                if len(tmp_data)<1024:
                    tmp_data+=chr(0).encode()*(1024-len(tmp_data))
                data=data[1024:]
                sock.send(tmp_data)
                i+=1
                window["-progress-bar-"].UpdateBar(i)  
            else:
                # sock.send(file.encode())
                sock.close()
                break
        window.write_event_value("-end-task-","file sended")
    except:
        window.write_event_value("-error-","error happened")

thread=None

while True:
    event,inputstr=window.read()
    if event==sg.WINDOW_CLOSED:
        break
    elif event=="send-btn" and not thread and inputstr["-PORT-"].isdigit() and \
        not inputstr["-IP-"].isalnum():
        window["-progress-bar-"].UpdateBar(0)
        file=inputstr["-FILE-"]
        ip=inputstr["-IP-"]
        port=int(inputstr["-PORT-"])
        thread=Thread(target=send_file,args=(file,ip,port))
        thread.start()
    elif event=="-error-":
        thread=None
        sg.PopupAutoClose("check you ip or port",title="error")
    elif event=='-end-task-':
        thread=None
        sg.popup("the file has been sent")
window.close()

 

سورس گیرنده : 

from threading import Thread
import PySimpleGUI as sg
from PySimpleGUI.PySimpleGUI import No, WIN_CLOSED
from socket import AF_INET, SOCK_STREAM, socket


def recieve_data(ip,port):
    global thread
    filedata=b""
    try:
        sock=socket(AF_INET,SOCK_STREAM)
        sock.bind((ip,port))
        sock.listen()
        sender,addr=sock.accept()
        while True:
            data=sender.recv(1024)
            if len(data)==0:
                sock.close()
                
                break
                        
                
                
                
            filedata+=data
        window.write_event_value("-end-task-",filedata)
    except:
        window.write_event_value("-problem-","problem happened")
        

layout=[
        [sg.Text("your ip:"),sg.InputText(key="-IP-")],
        [sg.Text("your port:"),sg.InputText(key="-PORT-")],
        [sg.Button("start recieving",key="-btn-recieve-",border_width=2,s=(11,2),pad=(0,15))],
        
]

window=sg.Window("recieve app",layout=layout,font="Arial 12",element_justification="c",size=(400,150))

thread=None
while True:
    event,inputstr=window.read()
    
    if event==sg.WIN_CLOSED:
        break
    elif event=="-btn-recieve-" and not thread and inputstr["-PORT-"].isdigit() and \
        not inputstr["-IP-"].isalnum():
        ip=inputstr["-IP-"]
        port=int(inputstr["-PORT-"])
        
        thread=Thread(target=recieve_data,args=(ip,port),daemon=True)
        thread.start()
        sg.popup_auto_close("waiting for sender...",auto_close_duration=2)
    elif event=="-problem-":
        thread=None
        sg.popup_no_titlebar("correct your ip or port")
    elif event=="-end-task-":
        data_file=inputstr[event]
        thread=None
        text=sg.popup_get_text("name of the file:","save file")
        with open(text,"wb") as file:
            file.write(data_file)
            sg.popup("file has been saved")
       

window.close()

 

جهت ارسال پروژه های خود :

پروژه ی خود به همراه توضیح آن  ارسال کنید به  : 

mrpythonblog@gmail.com


 Telegram Channel : @mrpythonblog

ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی