|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
||||
|
||||
|
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?
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
||||
|
||||
|
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. Last edited by netytan : December 4th, 2003 at 08:52 AM. |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
||||
|
||||
|
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<- |
|
#6
|
||||
|
||||
|
I havn't touched much on threads yet, but as i understand it a thread is a sub/child process which runs along side the main program (and other threads)..
so i guess threads let your program handle more than one thing at the same time!? think of it this way.. your program as like a hair, a the thread is a split end (soz, my sis is a hairdresser)Python modules that handle threading: http://www.python.org/doc/2.3.2/lib/module-thread.html http://www.python.org/doc/2.3.2/lib...-threading.html http://www.python.org/doc/2.3.2/lib...ummythread.html http://www.python.org/doc/2.3.2/lib...ythreading.html Mark. |
|
#7
|
||||
|
||||
|
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
![]() |
|
#8
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > send( ) function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|