File Transfer via Sockets in Python

Поділитися
Вставка
  • Опубліковано 21 гру 2024

КОМЕНТАРІ • 107

  • @missipgaming6174
    @missipgaming6174 2 роки тому +36

    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.

  • @BatteryProductions
    @BatteryProductions 6 місяців тому +1

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

  • @adventinfomax6903
    @adventinfomax6903 2 роки тому +3

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

  • @klausvonshnytke
    @klausvonshnytke 2 роки тому +14

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

    • @andme-tech102
      @andme-tech102 Рік тому

      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 Рік тому

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

    • @elimelechschreiber6937
      @elimelechschreiber6937 11 місяців тому

      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.

  • @scorpio3899
    @scorpio3899 2 роки тому +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 2 роки тому

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

    • @scorpio3899
      @scorpio3899 2 роки тому +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

  • @davidtindell950
    @davidtindell950 Рік тому +1

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

  • @ghost99slay
    @ghost99slay 2 роки тому

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

  • @lastdance2099
    @lastdance2099 2 роки тому +19

    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.

    • @bruhhhhh718
      @bruhhhhh718 Рік тому +5

      thanks for this knowledge

    • @FlexThoseMuscles
      @FlexThoseMuscles 7 днів тому

      Thanks a lot, this solved the particular problem I was encountering on my computer.

  • @gonzalogoded2089
    @gonzalogoded2089 8 місяців тому +5

    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

  • @minhajhossain337
    @minhajhossain337 Рік тому +1

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

  • @businessdissection130
    @businessdissection130 2 роки тому

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

  • @ethanberg1
    @ethanberg1 9 місяців тому

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

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 2 роки тому

    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.

  • @bruhhhhh718
    @bruhhhhh718 Рік тому +2

    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

  • @trido3815
    @trido3815 Місяць тому

    Great info. Thanks. BTW, for the sender, I think we can use the context manager at 'open' . Right?

  • @yunisguliyev3815
    @yunisguliyev3815 9 місяців тому

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

  • @shentrax
    @shentrax Рік тому +4

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

    • @MekeninzoUG
      @MekeninzoUG 7 місяців тому +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 6 місяців тому +1

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

    • @MekeninzoUG
      @MekeninzoUG 6 місяців тому +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.

  • @tommyhuffman7499
    @tommyhuffman7499 Рік тому

    Your channel is underrated

  • @felloforest
    @felloforest Рік тому

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

  • @olorundaremicheal8015
    @olorundaremicheal8015 2 роки тому +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.

  • @LHM1226
    @LHM1226 Рік тому

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

  • @EntrepreneurChips
    @EntrepreneurChips Рік тому

    Thank You NeuralNine🥰

  • @51swarajrohad87
    @51swarajrohad87 Рік тому +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 6 місяців тому +1

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

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 2 роки тому +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 💀💀💀💀💀

  • @FunueLian
    @FunueLian Рік тому

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

  • @ghostandry8789
    @ghostandry8789 11 місяців тому

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

  • @FramesByAkhil
    @FramesByAkhil 8 місяців тому

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

  • @MohammadRezaee-wh7zk
    @MohammadRezaee-wh7zk Рік тому

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

  • @Herzfeld10
    @Herzfeld10 Рік тому

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

  • @meraklmuskulpesent8313
    @meraklmuskulpesent8313 2 роки тому +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 2 роки тому +1

      i have the same problem

    • @alifurqan1375
      @alifurqan1375 2 роки тому +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 роки тому +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 Рік тому

      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.

  • @jansz1589
    @jansz1589 Рік тому

    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?

  • @ΠΑΝΟΣΚΩΣΤΟΠΟΥΛΟΣ-η3χ
    @ΠΑΝΟΣΚΩΣΤΟΠΟΥΛΟΣ-η3χ 4 місяці тому

    really nice video just a mistake i see. when dealing with large files/data you cant have a variable to include all the data. that is why we have buffers generally for memory purposes. when reading a chunk of the file this chunk should be either passed to another socket or saved in disk not RAM. In C when reading from a socket the number of characters being read is returned so you know when the file is done, and also the eof is 1 byte aka one character. dont know exactly why in this it is not familiar with how python handles files

  • @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

  • @filippocaregnato4098
    @filippocaregnato4098 Рік тому

    Does it work with surface? Like cv2 img?

  • @omarelesnawy1833
    @omarelesnawy1833 Рік тому

    Thank you, nice explanation

  • @sksahil4374
    @sksahil4374 3 місяці тому

    Nice approach

  • @sayan763
    @sayan763 Рік тому

    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 Рік тому

      On localhost

    • @sayan763
      @sayan763 Рік тому

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

  • @nkehbebey9182
    @nkehbebey9182 Рік тому

    Can this file be sent and received in any format

  • @MariamAbouzaid--
    @MariamAbouzaid-- 7 місяців тому

    how to send folder of images?

  • @juozas163
    @juozas163 2 роки тому

    Thank you so much! It work!

  • @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

  • @abdullahyousef3596
    @abdullahyousef3596 2 роки тому

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

  • @pablogiri812
    @pablogiri812 6 місяців тому

    Muchas gracias!!!!!!

  • @virendrakumar-ez2ue
    @virendrakumar-ez2ue Рік тому

    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

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

  • @tejasnikumbh776
    @tejasnikumbh776 3 місяці тому

    how to create file trafer website file tranfer website to mobile (pc to mobile vs mobile to pc)

  • @Roman.Pavelka
    @Roman.Pavelka 2 роки тому

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

    • @edwardlynch-milner8902
      @edwardlynch-milner8902 2 роки тому

      I'm wondering the same thing

    • @klausvonshnytke
      @klausvonshnytke 2 роки тому

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

    • @klausvonshnytke
      @klausvonshnytke 2 роки тому +4

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

  • @johnydoe6338
    @johnydoe6338 Рік тому

    now, how to secure it ?

  • @mehdismaeili3743
    @mehdismaeili3743 2 роки тому

    Excellent.

  • @ashleybishton742
    @ashleybishton742 Рік тому

    Great video

  • @sanymiran3845
    @sanymiran3845 Рік тому

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

  • @yashindane2844
    @yashindane2844 2 роки тому +2

    How to do the same in flask webapp?

    • @metantonio
      @metantonio 2 роки тому

      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"})

  • @QuransGems-zd1jd
    @QuransGems-zd1jd 4 місяці тому

    man where is the data we sent being stored? Can anybody access that data?

  • @ozkaninonlu2898
    @ozkaninonlu2898 2 роки тому +1

    Awesome

  • @arrozyadifalaqi9292
    @arrozyadifalaqi9292 2 роки тому

    I could fix tNice tutorials?

  • @119nidunkrishna3
    @119nidunkrishna3 3 місяці тому

    i was like when seeing transfered.exe 🤫😵

  • @philtoa334
    @philtoa334 2 роки тому

    Thx_.

  • @ahmedgamberli2250
    @ahmedgamberli2250 2 роки тому +1

    13. Comment

  • @raven-vr5yz
    @raven-vr5yz 4 місяці тому

    bro are you buying positive comments? i have never read such a crap

  • @daesk
    @daesk 5 місяців тому +3

    unreliable and bad code.

    • @TeriqueCarnegie
      @TeriqueCarnegie 5 місяців тому

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

  • @grgregrrgsgff1878
    @grgregrrgsgff1878 2 роки тому +1

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

    • @ga8r0
      @ga8r0 2 роки тому +1

      Hello, what you need?

    • @grgregrrgsgff1878
      @grgregrrgsgff1878 2 роки тому

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

    • @ga8r0
      @ga8r0 2 роки тому +1

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

    • @grgregrrgsgff1878
      @grgregrrgsgff1878 2 роки тому

      @@ga8r0 Ok😥 sorry for Disturbing

    • @ga8r0
      @ga8r0 2 роки тому

      @@grgregrrgsgff1878 don't worry, no problem