simple socket code

Jeremy Jones zanesdad at bellsouth.net
Thu Sep 30 13:00:56 EDT 2004


Nikola Bjelicic wrote:

>**** Post for FREE via your newsreader at post.usenet.com ****
>
>
>Please help me to put some content on apache + DAV server, whas wrong?
>
>[code]
>
>import socket, os, stat
>
>file=open('/home/nikola/somefile','rb')
>lenght=os.fstat(file.fileno())[stat.ST_SIZE]
>print lenght
>
>headers={}
>headers['Host']='localhost'
>headers['Authorization']='Basic bmlrb2xhOnRlc3Q='
>headers['Accept-Encoding']='identity'
>headers['Content-Type']='application/octet-stream'
>headers['Content-Encoding']='None'
>headers['Content-Lenght']=lenght
>
>
>data = '%s %s %s\r\n' %('PUT', '/dav/test1','HTTP/1.1')
>for header, value in headers.items():
>  data += '%s: %s\r\n'%(header,value)
>
>data += '\r\n'
>
>sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>sock.connect(('localhost', 80))
>sock.send(data)
>sock.send(file.read()) #doesnt work why?
>response=sock.recv(2000)
>print response
>sock.close()
>
>[/code]
>
>
>
>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> *** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
>                      http://www.usenet.com
>Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>  
>

Do you _really_ want to do this by hand with sockets?  I would recommend 
trying httplib:

http://www.python.org/doc/current/lib/httplib-examples.html

Jeremy Jones



More information about the Python-list mailing list