Python Forum
file transfer with sockets
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file transfer with sockets
#1
Hey guys i'm trying to get my server to be able to send my client files. Sorry for my horrible code i'm still pretty new and learning. Any replies will be appreciated! Thank you

Server:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import socket
import os
 
 
path = '/home/joshua/Downloads/hello.text'
 
def send(conn,path):
 
    if os.path.exists(path):
        f = open(path, 'rb')
        program = f.read(1024)
        while program != '':
            conn.send(program)
            program = f.read(1024)
        conn.send('done')
        f.close()
 
def connect():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('127.0.0.1', 8080))
    s.listen(1)
    conn,addr = s.accept()
     
    while True:
         
        command = raw_input('Shell> ')
         
        if 'terminate' in command:
            conn.send('terminate')
            conn.close()
         
        elif 'send' in command:
            send(conn,path)
             
        else:
            conn.send(command)
            conn.recv(1024)
 
def main():
    connect()
main()
Client:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import socket
import os
import subprocess
 
path = '/home/joshua/pyPrograms/file.txt'
 
def recieve(s, path,command):
         
        s.send(command)
        f = open(path, 'wb')
        while True:
            bits = s.recv(1024)
            print(bits)
            if bits.endswith('done'):
                f.close()
                break
            f.write(bits)
         
     
     
 
def connect():
     
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('127.0.0.1', 8080))
     
    while True:
         
        command = s.recv(1024)
         
        if 'send' in command:
            recieve(s,path)
         
         
         
         
        else:
            CMD = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            s.send( CMD.stdout.read() )
            s.send( CMD.stderr.read() )
             
def main():
    connect()
main()
Reply
#2
Could you tell us, what is your problem ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sockets interferring with USB ? epif18 0 3,324 Mar-19-2021, 07:45 PM
Last Post: epif18
  Just learning sockets etc... floatingshed 2 3,016 May-06-2020, 09:37 PM
Last Post: floatingshed
  Quick sockets question... ptrivino 2 3,088 Sep-26-2019, 08:51 PM
Last Post: ptrivino
  Sockets and Sendmail taintedsushi 2 3,110 Sep-02-2019, 12:51 PM
Last Post: venquessa
  Script Conversion 2.7 to 3 (sockets) Pumpernickel 1 3,180 Apr-10-2019, 04:26 PM
Last Post: Pumpernickel
  File Transfer deezy 5 15,238 Feb-25-2019, 02:41 PM
Last Post: Jaylord
  File transfer with xmodem CRC (1024) checksum Jhonbovi 3 9,904 Nov-08-2018, 09:01 AM
Last Post: Larz60+
  What sort of things can I write to learn about sockets marienbad 2 3,462 Oct-16-2018, 04:02 PM
Last Post: buran
  unix domain sockets Skaperen 8 6,596 Sep-02-2018, 07:02 PM
Last Post: Skaperen
  Trouble with sending raw sockets IronReign 2 5,048 Jun-01-2017, 08:26 AM
Last Post: camp0

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020