Problem using StringIO file object

J

Joshua Burvill

Hello,

I have the following script for printing something to a print server using
ftplib, but it seems that I have a problem with putting the content into the
file object as is shown by the following output from the script.

Any tips?

-Josh

########################
## SCRIPT STARTS HERE ##
########################

def mk_zeb_label():

from ftplib import FTP
import string, cStringIO




## Create file-handle to a "file-like" string
file_handle=cStringIO.StringIO()

printserver_ip="192.168.2.177"
portname="COM"
zpt_code="""
^XA
^cfd,36,20
^by3,,50
^fo470,50
^abn,48,30^fdREPAIR^fs
^fo50,150
^fdSome text goes here.^fs
^fdSome text goes here.^fs
^fdSome text goes here.^fs
^fo50,50
^bc^fd114567^fs
^XZ
"""


file_handle.write(zpt_code)
ftp=FTP(printserver_ip)
ftp.login("","")
ftp.set_pasv(0)

val=ftp.storlines("STOR " + portname, file_handle)

return val, file_handle.read()
 
M

Myles

Joshua Burvill said:
file_handle.write(zpt_code)

Okay, you've written to your file object, so your current "file
position" is at the end of the buffer, ready for more writes - reading
from it now will only produce an empty string.
You need to reset your file position to the beginning of the buffer.
ftp=FTP(printserver_ip)
ftp.login("","")
ftp.set_pasv(0)

I'd try putting in a
file_handle.seek(0)
here, so that your ftp object can read from the beginning
of the buffer
val=ftp.storlines("STOR " + portname, file_handle)

and another
file_handle.seek(0)
here so the read can also produce data.
return val, file_handle.read()

Regards, Myles.
 
M

Myles

Joshua Burvill said:
file_handle.write(zpt_code)

Okay, you've written to your file object, so your current "file
position" is at the end of the buffer, ready for more writes - reading
from it now will only produce an empty string.
You need to reset your file position to the beginning of the buffer.
ftp=FTP(printserver_ip)
ftp.login("","")
ftp.set_pasv(0)

I'd try putting in a
file_handle.seek(0)
here, so that your ftp object can read from the beginning
of the buffer
val=ftp.storlines("STOR " + portname, file_handle)

and another
file_handle.seek(0)
here so the read can also produce data.
return val, file_handle.read()

Regards, Myles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top