File Transfer via Sockets in Python

Поделиться
HTML-код
  • Опубликовано: 11 сен 2022
  • In this video, we learn how to transfer files via sockets in Python.
    ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
    📚 Programming Books & Merch 📚
    🐍 The Python Bible Book: www.neuralnine.com/books/
    💻 The Algorithm Bible Book: www.neuralnine.com/books/
    👕 Programming Merch: www.neuralnine.com/shop
    🌐 Social Media & Contact 🌐
    📱 Website: www.neuralnine.com/
    📷 Instagram: / neuralnine
    🐦 Twitter: / neuralnine
    🤵 LinkedIn: / neuralnine
    📁 GitHub: github.com/NeuralNine
    🎙 Discord: / discord
    🎵 Outro Music From: www.bensound.com/
  • НаукаНаука

Комментарии • 99

  • @missipgaming6174
    @missipgaming6174 Год назад +30

    You are by far my favorite source for Python lessons. 1. You have a solid intro. 2. Your videos are not too long. 3. Even though the videos are not that long, you are clear on your explanation. Thank you for doing this.

  • @adventinfomax6903
    @adventinfomax6903 Год назад +3

    You've actually got so quality stuff on your channel. Keep that up man!

  • @ghost99slay
    @ghost99slay Год назад

    Yo! Congrats on the 150k homie! Keep up the good work!! I love watching your videos

  • @minhajhossain337
    @minhajhossain337 Год назад +1

    I love your videos Florian. Because, you always explain everything very easily in a short time period 😊😊😊

  • @davidtindell950
    @davidtindell950 7 месяцев назад

    Thank you! I do like your inclusion of the progress bar!!

  • @ethanberg1
    @ethanberg1 4 месяца назад

    Thank you! You just made my life easier next week!

  • @felloforest
    @felloforest Год назад

    very informative. Not too brief but you get the concepts behind it.

  • @scorpio3899
    @scorpio3899 Год назад +7

    Great stuff, I'm working with a project using sockets and this might help me.
    And btw you can also send Python objects using the library pickle, this works if you need to send an array of data or some complex object you're working with

    • @donatellosnizzo100
      @donatellosnizzo100 Год назад

      saving an object in a file with pickle and then sending that file. nice one!!

    • @scorpio3899
      @scorpio3899 Год назад +2

      @@donatellosnizzo100 well it doesn't create a file, it just wraps an object and just send it, it helps when you don't wanna save too many files of images, objects or whatever you wanna send, so you can pass multiple array of data or anything else without wasting hard-drive space

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 Год назад

    Have you looked at passing open file descriptors over Unix-domain sockets? So for example an authentication process could control access to a particular directory, and grant selected client processes permissions to read/write files there. And all of this could be done without anybody needing root privileges.

  • @businessdissection130
    @businessdissection130 Год назад

    Honestly, I started watching your videos for the intro music. And stayed for the content.

  • @EntrepreneurChips
    @EntrepreneurChips Год назад

    Thank You NeuralNine🥰

  • @tommyhuffman7499
    @tommyhuffman7499 11 месяцев назад

    Your channel is underrated

  • @BatteryProductions
    @BatteryProductions Месяц назад

    you are using file_size only to print the size, you could also use that information to do the loop instead of a tag, just read data until data == file_size...

  • @juozas163
    @juozas163 Год назад

    Thank you so much! It work!

  • @klausvonshnytke
    @klausvonshnytke Год назад +13

    Just wondering, don't you end up with the string appended to the transferred file?

    • @andme-tech102
      @andme-tech102 11 месяцев назад

      I don't think so because the file data and string were sent as two separate transmissions so in the receiving end in receives file data first before the END string so when it reaches the end string it terminates writing to the file without actually including the END string

    • @klausvonshnytke
      @klausvonshnytke 11 месяцев назад

      @@andme-tech102 I tested this code and does get appended at the end.

    • @elimelechschreiber6937
      @elimelechschreiber6937 6 месяцев назад

      Yes of course.
      This is an obvious bug.
      The filesize would be larger, and a checksum test would obviously fail.
      Although an image and even simple executables should still work.

  • @omarelesnawy1833
    @omarelesnawy1833 Год назад

    Thank you, nice explanation

  • @bruhhhhh718
    @bruhhhhh718 Год назад +1

    i think it's important to note why you are using os.path.getsize vs file.__sizeof__ -- one is size on disk, the other size in memory

  • @LHM1226
    @LHM1226 Год назад

    What library would you recommend for FTP transfer? Would the socket best work with FTP?

  • @MohammadRezaee-wh7zk
    @MohammadRezaee-wh7zk Год назад

    Hi, Did you make your website server side by python?

  • @olorundaremicheal8015
    @olorundaremicheal8015 Год назад +1

    Thanks for this boss. I have been hoping to understand this for a long time. Please could you do a tutorial on how to connect to a remote cloud database with python. Thanks.

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 Год назад +1

    Congratz on 153k subs, I have been following you for a while now your videos are pretty good!
    Could you maybe make more videos about Java? Maybe Java networking or something like that using Spring?

    • @alexdelarge1845
      @alexdelarge1845 Год назад

      i second this sugestion. it would be awesome some java sockets tutorilas

    • @abdultheseekerofknowledge4453
      @abdultheseekerofknowledge4453 Год назад

      Who uses Java on this day and age, maybe try something like Node.js, GO, Rust

    • @oguzhantopaloglu9442
      @oguzhantopaloglu9442 Год назад

      @@abdultheseekerofknowledge4453 Those words of your are too bold for an argument that can be destroyed with a simple linkedin search lmao
      Java probably has more job offers than all those 3 combined xD

    • @abdultheseekerofknowledge4453
      @abdultheseekerofknowledge4453 Год назад

      @@oguzhantopaloglu9442 I think we should get real, Java is becoming like Php, in android app development Kotlin is far ahead of Java, for a backend development GO and Rust are far better options

    • @oguzhantopaloglu9442
      @oguzhantopaloglu9442 Год назад

      @@abdultheseekerofknowledge4453 "for a backend development GO and Rust"
      YEAH YOU WISH 💀💀💀💀💀

  • @yunisguliyev3815
    @yunisguliyev3815 4 месяца назад

    i am 70% sure that he recorded this video after break up. got the vibes

  • @user-fn8yz6pb6h
    @user-fn8yz6pb6h 7 месяцев назад

    Does the image have to be in the same file directory?

  • @pablogiri812
    @pablogiri812 29 дней назад

    Muchas gracias!!!!!!

  • @lastdance2099
    @lastdance2099 Год назад +14

    I'm seeing verbatim copies of this code being used all over the place and it has to stop. This code is filled with unreliable constructs. It exhibits every python sockets' bad practice I've seen in the last 10+ years. 1) If you don't check the return value from send() that's a bug. You should be using sendall() for all the sends that you're not checking the return value of, which in this case is all of them. 2) Although perhaps not a big deal for this particular example, assuming that a fixed chunk of bytes like 1024 will end on a utf-8-encoded unicode character boundary is incorrect; 3) The biggest mistake is your assumption the every single send() or sendall() will be read by a single recv on the server side. That's not how tcp works and not how python SOCK_STREAM sockets work. If you send 1022 bytes in one send and 25 bytes in the next send, then on the server side you may receive 50 bytes in your first recv(1024) and then 100 bytes on you next recv(1024). Or you might receive 1024 bytes in your first recv(1024) but then only 1 byte in your next recv(1024). In your case, you might receive the file name, the file size, and a big chunk of the file itself in your first recv(1024) that you're assigning to the variable file_name. See the following answer on stack overflow for more details: stackoverflow.com/questions/43420075/python-socket-not-receiving-without-sending/43420503#43420503.

  • @mehdismaeili3743
    @mehdismaeili3743 Год назад

    Excellent.

  • @ashleybishton742
    @ashleybishton742 Год назад

    Great video

  • @filippocaregnato4098
    @filippocaregnato4098 9 месяцев назад

    Does it work with surface? Like cv2 img?

  • @geraldmaina6137
    @geraldmaina6137 Год назад

    hi can py socket send malware through a phone /pc after getting a wifi ip addres through nmap and a wifi password thrugh netsh wlan in python

  • @nkehbebey9182
    @nkehbebey9182 Год назад

    Can this file be sent and received in any format

  • @FramesByAkhil
    @FramesByAkhil 3 месяца назад

    What if recived bytes contains end delimiter and next file name , if continuous files are transmitted

  • @ozkaninonlu2898
    @ozkaninonlu2898 Год назад +1

    Awesome

  • @51swarajrohad87
    @51swarajrohad87 9 месяцев назад +1

    bro there's a loop hole
    when a byte is received with ending as and saved in data it'll check file_bytes which will be false and else part will execute and the end will be appended in file_byte then theres no data to receive right so it should end there but it's not and loop iterates again and client.recv tries to
    recieve data which the sender has already finished sending. so it will be stuck there forever . Please correct me if i'm wrong

    • @BatteryProductions
      @BatteryProductions Месяц назад +1

      yes, avoid appending to anything, use file size instead compare to data size

  • @ghostandry8789
    @ghostandry8789 6 месяцев назад

    How can i recive the image name? Same as but with ?

  • @jansz1589
    @jansz1589 9 месяцев назад

    I have one little problem... When I send few back to back messages I end up with one big message that is essentially just all the messages concatenated. I don't know how to solve it and I see you somehow managed to do that. So my question is: how?

  • @squirtgun187
    @squirtgun187 Год назад +1

    @Gabro great work, concise, insightful and easy to follow. Have you, or anyone reading this, coded sockets for Bluetooth? I have been trying to get them to work, but so far no luck. I think my challenge is probably in the way that I am trying to connect/call, but I'm not sure and documentation is thin. Any help or insight would be appreciated.

    • @lastdance2099
      @lastdance2099 Год назад

      "...great work, concise, insightful and easy to follow..." and wrong.

    • @squirtgun187
      @squirtgun187 Год назад

      @@lastdance2099 ok, then how would it be done?
      I can scan / identify and sometimes connect, but I cannot / have not yet been able make a data transfer with Pybluez.
      I did find SimplpyBLE and that works well, but it won't show human friendly device names so I am left to guess which device I want to connect to.

    • @lastdance2099
      @lastdance2099 Год назад

      @@squirtgun187 I answer questions mainly on stackoverflow, but I'm not familiar with Pybluex or SimplpyBLE. I'm referring to the video, and I'm trying to mitigate the harmful effects of this video as I see people copying the buggy code presented in the video.

  • @abdullahyousef3596
    @abdullahyousef3596 Год назад

    How can we make it so we can access this socket through the internet for free instead of just local networks?

  • @Herzfeld10
    @Herzfeld10 8 месяцев назад

    please use a context manager when handling files (keyword "with")

  • @sayan763
    @sayan763 9 месяцев назад

    Just had one question if anybody could answer. This method only works over local host or can work over the wifi/internet as well?

    • @johnthompson4011
      @johnthompson4011 9 месяцев назад

      On localhost

    • @sayan763
      @sayan763 8 месяцев назад

      @@johnthompson4011 Any idea where I can learn to send files over the internet using Socket programming?? Or is it straight not possible. I want to learn it for my project. But my program is giving bind failure while using the global IP address.

  • @MariamAbouzaid--
    @MariamAbouzaid-- 2 месяца назад

    how to send folder of images?

  • @shentrax
    @shentrax 10 месяцев назад +4

    Curious - did you not have to strip the off the received file before writing the last bit of data?

    • @MekeninzoUG
      @MekeninzoUG 2 месяца назад +1

      Of course not. As soon as the tag is detected, the loop ends and nothing gets added to file_bytes down the detection line, so nothing to remove.

    • @BatteryProductions
      @BatteryProductions Месяц назад +1

      @@MekeninzoUG but he explained that maybe so he checks last 5 bytes ALREADY appended to file.. this is a bug...

    • @MekeninzoUG
      @MekeninzoUG Месяц назад +1

      @@BatteryProductions Yeah I never thought of that, but now I'm so far beyond this level.
      Using tags is a terrible practice. Much better to put a header on your data packets.

  • @johnydoe6338
    @johnydoe6338 Год назад

    now, how to secure it ?

  • @gonzalogoded2089
    @gonzalogoded2089 3 месяца назад

    I tried sending a 800Mb file and noticed it kept going slower and slower and eventually would take hours to pass, the reason being the internal fixed length of byte type variables and had to replicate all file every interaction.
    Found the solution. Substitute the line file_bytes = b"" for this one: file_bytes = bytearray(), from hours to transfer went to 8 seconds

  • @meraklmuskulpesent8313
    @meraklmuskulpesent8313 Год назад +1

    Thanks for the tutorial. For some reason, my code (receiver.py) throws this error:
    file_size = client.recv(1024).decode()
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 6: invalid start byte
    Could anyone tell me how to solve this?

    • @lilahmin
      @lilahmin Год назад +1

      i have the same problem

    • @alifurqan1375
      @alifurqan1375 Год назад +1

      Code runs fine when I run both scripts(server.py and client.py) on the same windows laptop.
      But when I run client.py on Raspberry Pi (linux OS) and server.py on laptop (windows OS) connected in LAN, I get the following error message :(
      UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 5: invalid start byte

    • @lastdance2099
      @lastdance2099 Год назад +2

      The is not a good video, most of the code shown is buggy and should not be emulated.

    • @edy1219
      @edy1219 Год назад

      It's because this isn't a good tutorial, it's literally trash.

    • @jansz1589
      @jansz1589 9 месяцев назад

      Your reciever recieves file data on top of the file size in one message. That is because of the way the TCP sockets work. If you send multiple messages one after another, there is high chance you will recieve them concatenated, assuming that your connection is not slow. So you essentially try to decode the file with utf-8 which obviously doesn't work.

  • @gassort008
    @gassort008 Год назад +1

    someone can helpme?
    file_size=client.recv(1024).decode()
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 6: invalid start byte

    • @edy1219
      @edy1219 Год назад +1

      Sorry mate you got tricked, this isn't a good tutorial the code here is really buggy, find another tutorial.

  • @Roman.Pavelka
    @Roman.Pavelka Год назад

    Nice! Would not get into the saved file? Seems it works though :)

    • @edwardlynch-milner8902
      @edwardlynch-milner8902 Год назад

      I'm wondering the same thing

    • @klausvonshnytke
      @klausvonshnytke Год назад

      Also noticed that. Looks like it does get appended but somehow it didn't break the exe

    • @klausvonshnytke
      @klausvonshnytke Год назад +4

      just recreated this and indeed "" does get appended. This program needs file_bytes = file_bytes[:-5] before writing the file

  • @RitikRaj-we2sc
    @RitikRaj-we2sc Год назад

    What if we want to send multiple files at the same time. How can we do that in parallel ?
    Please help me out here 🙏

    • @ericesquivel5298
      @ericesquivel5298 Год назад

      Probably use threads

    • @RitikRaj-we2sc
      @RitikRaj-we2sc Год назад

      @@ericesquivel5298 even if I use thread to send multiple file, still how will the receiver know which file is coming , how will it differentiate the incoming data

  • @sanymiran3845
    @sanymiran3845 Год назад

    how to do the same on different network?
    you know what i mean

  • @philtoa334
    @philtoa334 Год назад

    Thx_.

  • @virendrakumar-ez2ue
    @virendrakumar-ez2ue 7 месяцев назад

    This would not work if the sender and receiver are not on the same system and why are you writing in the image file? You should be omitting this while writing

  • @ahmedgamberli2250
    @ahmedgamberli2250 Год назад +1

    13. Comment

  • @yashindane2844
    @yashindane2844 Год назад +2

    How to do the same in flask webapp?

    • @metantonio
      @metantonio Год назад

      def allowed_file(filename):
      return '.' in filename and \
      filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
      @app.route('/upload', methods=['POST'])
      def upload_file():
      if request.method == 'POST':
      # check if the post request has the file part
      if 'file' not in request.files:
      return jsonify({"Msj": "No file"})
      file = request.files['file']
      # If the user does not select a file, the browser submits an
      # empty file without a filename.
      if file.filename == '':
      return jsonify({"Msj": "No selected file"})
      if file and allowed_file(file.filename):
      filename = secure_filename(file.filename)
      file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
      return jsonify({"Msj": "Archivo subido correctamente"})

  • @arrozyadifalaqi9292
    @arrozyadifalaqi9292 Год назад

    I could fix tNice tutorials?

  • @grgregrrgsgff1878
    @grgregrrgsgff1878 Год назад +1

    Hello, I am talking to neuralnine owner. Please response me

    • @ga8r0
      @ga8r0 Год назад +1

      Hello, what you need?

    • @grgregrrgsgff1878
      @grgregrrgsgff1878 Год назад

      @@ga8r0 I need to create a software like Netsupport school, Can you help me for that? Please

    • @ga8r0
      @ga8r0 Год назад +1

      @@grgregrrgsgff1878 Sorry, this is out of my area of expertise

    • @grgregrrgsgff1878
      @grgregrrgsgff1878 Год назад

      @@ga8r0 Ok😥 sorry for Disturbing

    • @ga8r0
      @ga8r0 Год назад

      @@grgregrrgsgff1878 don't worry, no problem

  • @daesk
    @daesk 19 дней назад +2

    unreliable and bad code.

    • @TeriqueCarnegie
      @TeriqueCarnegie День назад

      If possible can u point out what needs to be improved??