Cut file in packets and send them to server

Cut file in packets and send them to server

Postby hatemjapo » Sat Jan 18, 2014 10:32 pm

Hi

first of all, i am really new to python/repy but i need to write a work for my university to the end of the week

i must write a client, which should cut a file into packets and send them to a server.
How can i let python cut a file into packets?
hatemjapo
 
Posts: 10
Joined: Sat Jan 18, 2014 10:27 pm

Re: Cut file in packets and send them to server

Postby stranac » Sat Jan 18, 2014 11:46 pm

Well, this is my first time hearing of repy, so I'm not really sure what it can do/how it can do it...
Also, I have no idea what kind of client and server we're talking about, so I can't really be very specific.

In normal python code, I would do something like:
Code: Select all
with open(FILENAME) as f:
    for chunk in iter(lambda: f.read(CHUNK_SIZE), ''):
        send_to_server(chunk)

Basically, you can give .read() a maximum number of bytes to read, and use that in a loop.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1125
Joined: Thu Feb 07, 2013 3:42 pm

Re: Cut file in packets and send them to server

Postby hatemjapo » Tue Jan 21, 2014 4:56 pm

thx for replying!

as i said, i am very new to python, so i have no idea how to write things write
i am familiar with java and c++

so chunk are basically the kind of saying to cut things in python?
i created a funtion with a loop, to cut a file into pieces of 100Bytes and send them to a server

but i still have some questions:
how to tell python to cut them into 100 bytes?
is my code correct

Code: Select all
def filecutter (file) :
    with open (filename) as f:
        size = f.size
    while (size >0){
        for chunk in iter (lambda: f.read(100 ), ''):
            send_to_server (chunk)
        size = size - 100 
        if (size < 100) {
              f.read(size)
              size = 0}
        }




the goal of this programm is, to send the pakages to a server and he has to loose some of them
and i have to write more other programms to detect which pakage is missing and send it again, but for first i am trying to make python cut it
hatemjapo
 
Posts: 10
Joined: Sat Jan 18, 2014 10:27 pm

Re: Cut file in packets and send them to server

Postby stranac » Tue Jan 21, 2014 5:32 pm

Please read this to learn how to post code:
viewtopic.php?f=10&t=145

That braces stuff is not valid python code.
I suggest you go through the tutorial(it's meant for programmers) to learn the basics.

Also, I have no idea what you're trying to do here.
The code I've shown does all you want: it reads the entire file in chunks of size CHUNK_SIZE, and sends every chunk to the server(assuming you have written a send_to_server() function)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1125
Joined: Thu Feb 07, 2013 3:42 pm

Re: Cut file in packets and send them to server

Postby hatemjapo » Tue Jan 21, 2014 6:02 pm

i thought i have to define how big the chunks should be and make a loop to cut the whole file thats why i used while and the if sentence

and i see, i just need to make a send_to_server function

in the end, i should have 2 files, one the client who reads a file, cut it in pieces and send them to a server
and the other one is the server, who gets the files and reunite them to one file

but the thing is, i need a probability (that i can choose) for loosing pakages, so that not alle the packages can be send to the server (or gone lost while sending)
thien i need to configure my client.py with a redundancy system, that allows to send a package more than one time

then i have to write a simple XOR Forward error correction to see if a package is missing, to do that i will have to send a package with the info how much packages has been sent and if something is missing the resend

and then i have to test this whole shit with a adifferent probability
thats what i have to do...
hatemjapo
 
Posts: 10
Joined: Sat Jan 18, 2014 10:27 pm

Re: Cut file in packets and send them to server

Postby hatemjapo » Thu Jan 23, 2014 8:37 pm

can u explain me what chunk_size means?

and also whats the meaingn of chunk in iter (lambda??

i am trying to understand how to handle this
hatemjapo
 
Posts: 10
Joined: Sat Jan 18, 2014 10:27 pm

Re: Cut file in packets and send them to server

Postby stranac » Thu Jan 23, 2014 8:43 pm

CHUNK_SIZE is the amount of data you want to read on each pass of the loop.
http://docs.python.org/2/library/stdtyp ... #file.read

chunk is one of those read pieces of data.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1125
Joined: Thu Feb 07, 2013 3:42 pm

Re: Cut file in packets and send them to server

Postby hatemjapo » Fri Jan 24, 2014 1:39 pm

thx for replying

but now i am a little bit confused
does i really write chunk_size? or do i have to change it with the amount i need?
like when i need it in 100Bytes pieces i wirte
Code: Select all
f.read(100))


and other question:
i need to make a function for sending the files to a server (the server is just a py file)
how do i call the function? i mean with which parameterers?

can i write
Code: Select all
def sendtoserver (chunk) :


and how do i write to send the file to an other python file?

srry, i have some silly question but i am not used to python and i have to hand it in tonight, if someone is free and can help me for a few hours that would be nice, i would also pay for it!!
contact me plz
hatemjapo
 
Posts: 10
Joined: Sat Jan 18, 2014 10:27 pm

Re: Cut file in packets and send them to server

Postby stranac » Fri Jan 24, 2014 2:02 pm

hatemjapo wrote:does i really write chunk_size? or do i have to change it with the amount i need?

You can just write a number, yes.

hatemjapo wrote:can i write
Code: Select all
def sendtoserver (chunk) :

Yes, that is generally how you define a function(that's not calling it)

hatemjapo wrote:and how do i write to send the file to an other python file?

The information you have given is not enough to know this.
You do it however this server expects you to.
Can't tell more, since I have no idea how this server is implemented, or what it expects.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1125
Joined: Thu Feb 07, 2013 3:42 pm

Re: Cut file in packets and send them to server

Postby hatemjapo » Fri Jan 24, 2014 2:09 pm

the server is not a real server, its just server.py file (we just name it server)
and its yet an empty file...

i am thinking of def a function in server.py which awaits a chunk, and if it gets it, so it returns a boolean with true
but i am stick on how to send and wirte boolean in python~~~
hatemjapo
 
Posts: 10
Joined: Sat Jan 18, 2014 10:27 pm

Re: Cut file in packets and send them to server

Postby stranac » Fri Jan 24, 2014 2:31 pm

Where is this "server" located?
How are you supposed to connect to it?
Have you been given any actual instructions?

Also, you wouldn't really be sending a boolean, you would just send a string, and convert it to a boolean however you like.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1125
Joined: Thu Feb 07, 2013 3:42 pm

Re: Cut file in packets and send them to server

Postby hatemjapo » Fri Jan 24, 2014 2:41 pm

i think you still didnt get it

the server, is not a real server, its just a python file called server, its a server.py

its a file on my computer, write beside my client.py

that means, all i have to do is to send this piece of file to an other python file, from client.py to server.py
and server.py should reconnect the pieces

i hope its clear now...
hatemjapo
 
Posts: 10
Joined: Sat Jan 18, 2014 10:27 pm

Re: Cut file in packets and send them to server

Postby stranac » Fri Jan 24, 2014 2:53 pm

I see. Then why in hell is this in the Networking section?

That actually might make things more complicated...
Sounds like you need to run the client and server as different processes, and make those processes communicate with each other.
That could be pretty complicated to do, unless you can use 3rd party libraries.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1125
Joined: Thu Feb 07, 2013 3:42 pm


Return to Networking

Who is online

Users browsing this forum: No registered users and 1 guest