Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old December 4th, 2003, 03:06 AM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonder.
Posts: 5,537 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Weeks 1 Day 21 h 42 m
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook
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?

Reply With Quote
  #2  
Old December 4th, 2003, 08:34 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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

Reply With Quote
  #3  
Old December 4th, 2003, 08:47 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #4  
Old December 4th, 2003, 09:56 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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.

Reply With Quote
  #5  
Old December 4th, 2003, 03:28 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonder.
Posts: 5,537 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Weeks 1 Day 21 h 42 m
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook
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<-

Reply With Quote
  #6  
Old December 4th, 2003, 04:49 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #7  
Old December 4th, 2003, 06:54 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonder.
Posts: 5,537 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1Folding Points: 108660 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Weeks 1 Day 21 h 42 m
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook
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

Reply With Quote
  #8  
Old December 5th, 2003, 04:20 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > send( ) function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |