The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
send( ) function
Discuss send( ) function in the Python Programming forum on Dev Shed. send( ) function Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 4th, 2003, 03:06 AM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
|
send( ) function
Ok i want to create some kind of ftp clone.. but i have one question. I was tring to send a file using the function send() ( socket) but when i do so i get...
Traceback (most recent call last):
File "scripts/2.py", line 17, in ?
client.send(file("C:\\Python23\\scripts\\1.py","r"))
TypeError: send() argument 1 must be string or read-only buffer, not file
I know this is not the way to do it.. but i just hade to try it out, now is there another way that i can send some one a file when lets say .. if they was to type in get blah.(txt,py,c,php,etc)
or do i have to download a module for this?
|

December 4th, 2003, 08:34 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Hi xlordt,
You need something that knows the FTP protocol. Socket is too low down for that. There is a twisted FTP module but that may be too much!
A basic approach could be to try the Simple http server instead - then your client can simply point their browser at the URL and view/save files.
In 7 lines (one is a print statement!) Fredrik Lundh's Python Standard Library gives a functional server. This could be run as a background thread perhaps (as long as it's not a thread in a GUI application).
Code:
import SimpleHTTPServer
import SocketServer
PORT = 8181
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("",PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
Grim
|

December 4th, 2003, 08:47 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
The Error you got actually makes allot of sence  , send() wants a string! Try doing file(.... 'r').read()
I really wouldn't do this with raw sockets, ftplib is much easier to use.. http://www.python.org/doc/2.3.2/lib/module-ftplib.html
I'll try catch you on yahoo latter!
Mark.
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : December 4th, 2003 at 08:52 AM.
|

December 4th, 2003, 09:56 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
FTPlib is great as a client. If you need to know what is going on some tricks can be used:
To monitor file transfer rates you can create a file like wrapper object for the files so that all read/writes passed through them.
I found that most but not all FTP/socket exceptions are explicitly handled in the library so if you get a failure in connection its not always clear why.
If it's a server you want then FTPlib is not it I'm afraid
Grim.
|

December 4th, 2003, 03:28 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
|
hmm so i have some kind of choice here.. i know i have done this in c, and im tring to do it in python... but i will try the ftp one.. Another thing is i never paid mutch attantion to this thread stuff
can you tell me exactly that this means.. ->thread<-
|

December 4th, 2003, 04:49 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|

December 4th, 2003, 06:54 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
rotf so is my sister in law :P and i work in a beauty salon my self as a receptionist ... so Imagen all the chicks asses i get to see 
|

December 5th, 2003, 04:20 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Verbosity-switch ON!
When you run a python program it is the main thread of code. By using the thread or threading modules you can start another bunch of code in a separate thread. Say you wanted to calculate all the prime numbers! this woud take a long time and you would not want to sprinkle you prime code with checks for user input. One way to avoid this would be to write your user interface in the main thread and launch the prime calculator as a new thread of code. These two threads will run in parralell with the CPU switching periodically between them to allow each thread to be processed.
The threads share the same memory so global variables and other objects are available to both threads. This is fine until you start changing variables. Passing information between threads is easy but it you have to take care that threads are not trying to update the same variable at the same time (you can use a lock to prevent this).
The sample below starts two web servers so it has three threads running:
Code:
import threading
import SimpleHTTPServer
import SocketServer
#Create a thread based class to run the a web server
class MyWeb(threading.Thread):
def run(self):
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("",self.web_port), Handler)
print "Webserver at port", self.web_port
httpd.serve_forever()
web1 = MyWeb()
web1.web_port = 8181
web2 = MyWeb()
web2.web_port = 8282
#The start method calls the run method
web1.start()
web2.start()
#You cannot guarantee which thread will print first!
#point browser at http://localhost:8181 and http://localhost:8282
#In main thread do something else like create a MOTD file
text = raw_input("Enter the Message-of-the-day:")
f=open("MOTD.txt","w")
f.write(text)
f.close()
Grim
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|