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.
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...
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
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.
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 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
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.
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
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.
@@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.
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.
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
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?
@@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
@@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
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?
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
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.
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?
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
@@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.
@@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
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
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"})
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.
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...
You've actually got so quality stuff on your channel. Keep that up man!
Just wondering, don't you end up with the string appended to the transferred file?
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
@@andme-tech102 I tested this code and does get appended at the end.
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.
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
saving an object in a file with pickle and then sending that file. nice one!!
@@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
Thank you! I do like your inclusion of the progress bar!!
Yo! Congrats on the 150k homie! Keep up the good work!! I love watching your videos
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.
thanks for this knowledge
Thanks a lot, this solved the particular problem I was encountering on my computer.
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
I love your videos Florian. Because, you always explain everything very easily in a short time period 😊😊😊
Honestly, I started watching your videos for the intro music. And stayed for the content.
Thank you! You just made my life easier next week!
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.
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
Great info. Thanks. BTW, for the sender, I think we can use the context manager at 'open' . Right?
i am 70% sure that he recorded this video after break up. got the vibes
Curious - did you not have to strip the off the received file before writing the last bit of data?
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.
@@MekeninzoUG but he explained that maybe so he checks last 5 bytes ALREADY appended to file.. this is a bug...
@@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.
Your channel is underrated
very informative. Not too brief but you get the concepts behind it.
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.
MySQL database?
What library would you recommend for FTP transfer? Would the socket best work with FTP?
Thank You NeuralNine🥰
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
yes, avoid appending to anything, use file size instead compare to data size
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?
i second this sugestion. it would be awesome some java sockets tutorilas
Who uses Java on this day and age, maybe try something like Node.js, GO, Rust
@@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
@@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
@@abdultheseekerofknowledge4453 "for a backend development GO and Rust"
YEAH YOU WISH 💀💀💀💀💀
Does the image have to be in the same file directory?
How can i recive the image name? Same as but with ?
What if recived bytes contains end delimiter and next file name , if continuous files are transmitted
Hi, Did you make your website server side by python?
please use a context manager when handling files (keyword "with")
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?
i have the same problem
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
The is not a good video, most of the code shown is buggy and should not be emulated.
It's because this isn't a good tutorial, it's literally trash.
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.
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?
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
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
Does it work with surface? Like cv2 img?
Thank you, nice explanation
Nice approach
Just had one question if anybody could answer. This method only works over local host or can work over the wifi/internet as well?
On localhost
@@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.
Can this file be sent and received in any format
how to send folder of images?
Thank you so much! It work!
What if we want to send multiple files at the same time. How can we do that in parallel ?
Please help me out here 🙏
Probably use threads
@@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
How can we make it so we can access this socket through the internet for free instead of just local networks?
Muchas gracias!!!!!!
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
someone can helpme?
file_size=client.recv(1024).decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 6: invalid start byte
Sorry mate you got tricked, this isn't a good tutorial the code here is really buggy, find another tutorial.
how to create file trafer website file tranfer website to mobile (pc to mobile vs mobile to pc)
Nice! Would not get into the saved file? Seems it works though :)
I'm wondering the same thing
Also noticed that. Looks like it does get appended but somehow it didn't break the exe
just recreated this and indeed "" does get appended. This program needs file_bytes = file_bytes[:-5] before writing the file
now, how to secure it ?
Excellent.
Great video
how to do the same on different network?
you know what i mean
port forawding
How to do the same in flask webapp?
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"})
man where is the data we sent being stored? Can anybody access that data?
Awesome
I could fix tNice tutorials?
i was like when seeing transfered.exe 🤫😵
Thx_.
13. Comment
bro are you buying positive comments? i have never read such a crap
unreliable and bad code.
If possible can u point out what needs to be improved??
Hello, I am talking to neuralnine owner. Please response me
Hello, what you need?
@@ga8r0 I need to create a software like Netsupport school, Can you help me for that? Please
@@grgregrrgsgff1878 Sorry, this is out of my area of expertise
@@ga8r0 Ok😥 sorry for Disturbing
@@grgregrrgsgff1878 don't worry, no problem