ผมเขียนโปรแกรมส่งอาจารครับ เป็น โปรแกรม Upload Dowload โดยใช้ภาษา python
ซึ่งผมเขียนบน linux ครับ
ปัญหาก็คือ บางครั้ง พออัพโหลดข้อมูลแล้ว ทำได้ แต่บางครั้งทก็ำไม่ได้ครับ ผมเลยงงๆๆ
ยังไงผู้รู้ลองเอาโค๊ดนี้ไปรันดูนะครับ
./server.py --port 7777 <--- ผมใช้วิธีการรันแบบนี้ ไฟล์นี้เป็นไฟล์ server นะครับ
#! /usr/bin/python
# Server.py
import socket,sys
import os,stat
def download():
filedownload = conn.recv(1024)
print "%s" % (filedownload)
try:
file=open(filedownload,'rb')
conn.send('1')
file_name = filedownload
file_stat = os.stat(file_name)
fsize = file_stat[stat.ST_SIZE]
n=fsize/1024
m=fsize-(n*1024)
print "Senting %d 1024 byte packets, and one %d byte packate" % (n,m)
conn.send("%d" %(fsize))
for i in range(0,n):
file1=file.read(1024)
if len(file1)!=1024:
print "oh no short read :-( %d bytes" % (len(file1))
conn.send(file1)
if m>0:
file1=file.read(m)
conn.send(file1)
file.close()
except IOError:
conn.send('0')
def upload():
fileupload=conn.recv(1024)
print "%s" % (fileupload)
buf=conn.recv(8)
filesizeup=int(buf)
n=filesizeup/1024
m=filesizeup-(n*1024)
print "input %d 1024 byte packets, and one %d byte packate" % (n,m)
dataFile = open(fileupload,"wb")
countsize=0
for i in range(0,n):
dataUp=conn.recv(1024)
dataFile.writelines(dataUp)
countsize+=len(dataUp)
if m>0:
dataUp=conn.recv(m)
dataFile.writelines(dataUp)
countsize+=len(dataUp)
if countsize == filesizeup:
conn.send('1')
else:
conn.send('0')
dataFile.close()
print "Server running...."
HOST = '' # Symbolic name meaning all available interfaces
PORT = int(sys.argv[2]) # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if data=='d':
print "Server wait..."
download()
if data=='u':
print "Server wait..."
upload()
if data=='exit': break
else:
continue
conn.close()
ปัญหา python ครับ เกี่ยวกับเรื่อง socket ครับ
Started by
newnhons
, Jan 19 2010 05:20 AM
3 replies to this topic
#1
Posted 19 January 2010 - 05:20 AM
#2
Posted 19 January 2010 - 05:23 AM
ส่วนอันนี้เป็น client ครับ
./client.py --port 7777
#! /usr/bin/python
# client.py
import socket
import sys
import os,stat
def download(filedownload):
s.send(filedownload)
state=s.recv(4)
if state == '0':
print
print "Not file name :-( "
else:
try:
buf=s.recv(8)
filesize=int(buf)
print "%s bytes" % (buf)
print "Download %d bytes" % (filesize)
n=filesize/1024
m=filesize-(n*1024)
filename = filedownload
dataFile = open(filename,"wb")
fsize=0
for i in range(0,n):
dataDown=s.recv(1024)
dataFile.writelines(dataDown)
fsize+=len(dataDown)
perDown=(fsize*100)/filesize
print "Downloading %d%%" % (perDown)
if m>0: # ok, we have a few more bytes but nof full packet
dataDown=s.recv(m)
dataFile.writelines(dataDown)
fsize+=len(dataDown)
perDown=(fsize*100)/filesize
print "Downloading %d%%" % (perDown)
print "Download Complzzz"
print
dataFile.close()
except IOError:
print "Network Error!!. Download again.."
def upload(fileupload):
try:
dataUp = open(fileupload,"rb")
file_name = fileupload
file_stat = os.stat(file_name)
filesizeup = file_stat[stat.ST_SIZE]
n=filesizeup/1024
m=filesizeup-(n*1024)
s.send(fileupload)
s.send("%d" %(filesizeup))
countsize=0
for i in range(0,n):
dataupload = dataUp.read(1024)
s.send(dataupload)
countsize+=len(dataupload)
perUp=(countsize*100)/filesizeup
print "Uploading %d%%" % (perUp)
dataupload = dataUp.read(m)
s.send(dataupload)
countsize+=len(dataupload)
perUp=(countsize*100)/filesizeup
print "Uploading %d%%" % (perUp)
chack=s.recv(1024)
if chack == '1':
print "upload Complzzz"
else:
print "upload Error"
dataUp.close()
except IOError:
print "Not file name :-("
HOST = 'localhost' # The remote host
PORT = int(sys.argv[2]) # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while 1:
print "Upload or download [U/D]?"
print "ftp : "
f=sys.stdin.readline()
f=f.split('\n')
if f[0]=='u' or f[0]=='U':
s.send('u')
print "What file to upload :"
up=sys.stdin.readline()
up=up.split('\n')
upload(up[0])
if f[0]=='d' or f[0]=='D':
s.send('d')
print "What file to download :"
down=sys.stdin.readline()
down=down.split('\n')
download(down[0])
if f[0]=='exit':
s.send(f[0])
break
else:
continue
s.close()
ก็ขอขอบคุณร่วงน่าเลยครับ
./client.py --port 7777
#! /usr/bin/python
# client.py
import socket
import sys
import os,stat
def download(filedownload):
s.send(filedownload)
state=s.recv(4)
if state == '0':
print "Not file name :-( "
else:
try:
buf=s.recv(8)
filesize=int(buf)
print "%s bytes" % (buf)
print "Download %d bytes" % (filesize)
n=filesize/1024
m=filesize-(n*1024)
filename = filedownload
dataFile = open(filename,"wb")
fsize=0
for i in range(0,n):
dataDown=s.recv(1024)
dataFile.writelines(dataDown)
fsize+=len(dataDown)
perDown=(fsize*100)/filesize
print "Downloading %d%%" % (perDown)
if m>0: # ok, we have a few more bytes but nof full packet
dataDown=s.recv(m)
dataFile.writelines(dataDown)
fsize+=len(dataDown)
perDown=(fsize*100)/filesize
print "Downloading %d%%" % (perDown)
print "Download Complzzz"
dataFile.close()
except IOError:
print "Network Error!!. Download again.."
def upload(fileupload):
try:
dataUp = open(fileupload,"rb")
file_name = fileupload
file_stat = os.stat(file_name)
filesizeup = file_stat[stat.ST_SIZE]
n=filesizeup/1024
m=filesizeup-(n*1024)
s.send(fileupload)
s.send("%d" %(filesizeup))
countsize=0
for i in range(0,n):
dataupload = dataUp.read(1024)
s.send(dataupload)
countsize+=len(dataupload)
perUp=(countsize*100)/filesizeup
print "Uploading %d%%" % (perUp)
dataupload = dataUp.read(m)
s.send(dataupload)
countsize+=len(dataupload)
perUp=(countsize*100)/filesizeup
print "Uploading %d%%" % (perUp)
chack=s.recv(1024)
if chack == '1':
print "upload Complzzz"
else:
print "upload Error"
dataUp.close()
except IOError:
print "Not file name :-("
HOST = 'localhost' # The remote host
PORT = int(sys.argv[2]) # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while 1:
print "Upload or download [U/D]?"
print "ftp : "
f=sys.stdin.readline()
f=f.split('\n')
if f[0]=='u' or f[0]=='U':
s.send('u')
print "What file to upload :"
up=sys.stdin.readline()
up=up.split('\n')
upload(up[0])
if f[0]=='d' or f[0]=='D':
s.send('d')
print "What file to download :"
down=sys.stdin.readline()
down=down.split('\n')
download(down[0])
if f[0]=='exit':
s.send(f[0])
break
else:
continue
s.close()
ก็ขอขอบคุณร่วงน่าเลยครับ
#3
Posted 19 January 2010 - 03:22 PM
ฮือๆๆๆ เงียบกลิ่บเยยยยย
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users













