No video

File Transfer via Sockets in Python

  Рет қаралды 59,467

NeuralNine

NeuralNine

Күн бұрын

In this video, we learn how to transfer files via sockets in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: www.neuralnine...
💻 The Algorithm Bible Book: www.neuralnine...
👕 Programming Merch: www.neuralnine...
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine...
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/Neu...
🎙 Discord: / discord
🎵 Outro Music From: www.bensound.com/

Пікірлер: 102
@missipgaming6174
@missipgaming6174 Жыл бұрын
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 2 ай бұрын
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 Жыл бұрын
You've actually got so quality stuff on your channel. Keep that up man!
@lastdance2099
@lastdance2099 Жыл бұрын
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 Жыл бұрын
thanks for this knowledge
@scorpio3899
@scorpio3899 Жыл бұрын
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 Жыл бұрын
@@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
@klausvonshnytke
@klausvonshnytke Жыл бұрын
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 8 ай бұрын
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.
@businessdissection130
@businessdissection130 Жыл бұрын
Honestly, I started watching your videos for the intro music. And stayed for the content.
@minhajhossain337
@minhajhossain337 Жыл бұрын
I love your videos Florian. Because, you always explain everything very easily in a short time period 😊😊😊
@ghost99slay
@ghost99slay Жыл бұрын
Yo! Congrats on the 150k homie! Keep up the good work!! I love watching your videos
@ethanberg1
@ethanberg1 5 ай бұрын
Thank you! You just made my life easier next week!
@yunisguliyev3815
@yunisguliyev3815 6 ай бұрын
i am 70% sure that he recorded this video after break up. got the vibes
@davidtindell950
@davidtindell950 8 ай бұрын
Thank you! I do like your inclusion of the progress bar!!
@olorundaremicheal8015
@olorundaremicheal8015 Жыл бұрын
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.
@thepoorsultan5112
@thepoorsultan5112 Жыл бұрын
MySQL database?
@felloforest
@felloforest Жыл бұрын
very informative. Not too brief but you get the concepts behind it.
@user-jm9bz8ko7x
@user-jm9bz8ko7x 5 күн бұрын
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
@tommyhuffman7499
@tommyhuffman7499 Жыл бұрын
Your channel is underrated
@bruhhhhh718
@bruhhhhh718 Жыл бұрын
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
@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.
@shentrax
@shentrax 11 ай бұрын
Curious - did you not have to strip the off the received file before writing the last bit of data?
@MekeninzoUG
@MekeninzoUG 3 ай бұрын
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 2 ай бұрын
@@MekeninzoUG but he explained that maybe so he checks last 5 bytes ALREADY appended to file.. this is a bug...
@MekeninzoUG
@MekeninzoUG 2 ай бұрын
@@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.
@oguzhantopaloglu9442
@oguzhantopaloglu9442 Жыл бұрын
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 💀💀💀💀💀
@gonzalogoded2089
@gonzalogoded2089 4 ай бұрын
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
@Herzfeld10
@Herzfeld10 9 ай бұрын
please use a context manager when handling files (keyword "with")
@pablogiri812
@pablogiri812 2 ай бұрын
Muchas gracias!!!!!!
@EntrepreneurChips
@EntrepreneurChips Жыл бұрын
Thank You NeuralNine🥰
@omarelesnawy1833
@omarelesnawy1833 Жыл бұрын
Thank you, nice explanation
@squirtgun187
@squirtgun187 Жыл бұрын
@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.
@LHM1226
@LHM1226 Жыл бұрын
What library would you recommend for FTP transfer? Would the socket best work with FTP?
@FramesByAkhil
@FramesByAkhil 4 ай бұрын
What if recived bytes contains end delimiter and next file name , if continuous files are transmitted
@ghostandry8789
@ghostandry8789 7 ай бұрын
How can i recive the image name? Same as but with ?
@virendrakumar-ez2ue
@virendrakumar-ez2ue 8 ай бұрын
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
@juozas163
@juozas163 Жыл бұрын
Thank you so much! It work!
@jansz1589
@jansz1589 10 ай бұрын
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?
@user-fn8yz6pb6h
@user-fn8yz6pb6h 8 ай бұрын
Does the image have to be in the same file directory?
@51swarajrohad87
@51swarajrohad87 10 ай бұрын
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 2 ай бұрын
yes, avoid appending to anything, use file size instead compare to data size
@MariamAbouzaid--
@MariamAbouzaid-- 4 ай бұрын
how to send folder of images?
@MohammadRezaee-wh7zk
@MohammadRezaee-wh7zk Жыл бұрын
Hi, Did you make your website server side by python?
@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
@meraklmuskulpesent8313
@meraklmuskulpesent8313 Жыл бұрын
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 Жыл бұрын
i have the same problem
@alifurqan1375
@alifurqan1375 Жыл бұрын
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 Жыл бұрын
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 10 ай бұрын
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.
@mehdismaeili3743
@mehdismaeili3743 Жыл бұрын
Excellent.
@filippocaregnato4098
@filippocaregnato4098 10 ай бұрын
Does it work with surface? Like cv2 img?
@abdullahyousef3596
@abdullahyousef3596 Жыл бұрын
How can we make it so we can access this socket through the internet for free instead of just local networks?
@ashleybishton742
@ashleybishton742 Жыл бұрын
Great video
@gassort008
@gassort008 Жыл бұрын
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 Жыл бұрын
Sorry mate you got tricked, this isn't a good tutorial the code here is really buggy, find another tutorial.
@nkehbebey9182
@nkehbebey9182 Жыл бұрын
Can this file be sent and received in any format
@ozkaninonlu2898
@ozkaninonlu2898 Жыл бұрын
Awesome
@QuransGems-zd1jd
@QuransGems-zd1jd 28 күн бұрын
man where is the data we sent being stored? Can anybody access that data?
@sayan763
@sayan763 10 ай бұрын
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 10 ай бұрын
On localhost
@sayan763
@sayan763 10 ай бұрын
@@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.
@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
@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 Жыл бұрын
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 ?
@yashindane2844
@yashindane2844 Жыл бұрын
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"})
@sanymiran3845
@sanymiran3845 Жыл бұрын
how to do the same on different network? you know what i mean
@majonezowy4522
@majonezowy4522 Жыл бұрын
port forawding
@arrozyadifalaqi9292
@arrozyadifalaqi9292 Жыл бұрын
I could fix tNice tutorials?
@philtoa334
@philtoa334 Жыл бұрын
Thx_.
@daesk
@daesk Ай бұрын
unreliable and bad code.
@TeriqueCarnegie
@TeriqueCarnegie Ай бұрын
If possible can u point out what needs to be improved??
@ahmedgamberli2250
@ahmedgamberli2250 Жыл бұрын
13. Comment
@raven-vr5yz
@raven-vr5yz 27 күн бұрын
bro are you buying positive comments? i have never read such a crap
@grgregrrgsgff1878
@grgregrrgsgff1878 Жыл бұрын
Hello, I am talking to neuralnine owner. Please response me
@ga8r0
@ga8r0 Жыл бұрын
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 Жыл бұрын
@@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
Python Sockets Simply Explained
39:33
NeuralNine
Рет қаралды 160 М.
Encrypted File Transfer via Sockets in Python
19:54
NeuralNine
Рет қаралды 21 М.
How I Did The SELF BENDING Spoon 😱🥄 #shorts
00:19
Wian
Рет қаралды 37 МЛН
If Barbie came to life! 💝
00:37
Meow-some! Reacts
Рет қаралды 78 МЛН
Ik Heb Aardbeien Gemaakt Van Kip🍓🐔😋
00:41
Cool Tool SHORTS Netherlands
Рет қаралды 9 МЛН
Magic? 😨
00:14
Andrey Grechka
Рет қаралды 20 МЛН
The Most Legendary Programmers Of All Time
11:49
Aaron Jack
Рет қаралды 552 М.
How To Use FTP In Python
4:02
Taylor's Software
Рет қаралды 850
Create Stunning Python GUIs in 10 Minutes With Drag & Drop
11:38
Coding Is Fun
Рет қаралды 56 М.
I run untested, viewer-submitted code on my 500-LED christmas tree.
45:17
Linux File System/Structure Explained!
15:59
DorianDotSlash
Рет қаралды 4,1 МЛН
I've been using Redis wrong this whole time...
20:53
Dreams of Code
Рет қаралды 354 М.
The most important Python script I ever wrote
19:58
John Watson Rooney
Рет қаралды 187 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 299 М.
Coding Encrypted Chat in Python
20:33
NeuralNine
Рет қаралды 46 М.
How I Did The SELF BENDING Spoon 😱🥄 #shorts
00:19
Wian
Рет қаралды 37 МЛН