simple socket code

From: Nikola Bjelicic (nikola_at_bonbon.net)
Date: 09/30/04


Date: Thu, 30 Sep 2004 18:53:30 +0200


**** 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
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



Relevant Pages

  • Re: Preventing URLConnection from buffering entire output stream
    ... you can do this going at the lower [Client] Socket level. ... Write the HTTP headers. ... Calling flushon the OutputStream ...
    (comp.lang.java.programmer)
  • Re: simple socket code
    ... >import socket, os, stat ... >for header, value in headers.items: ...
    (comp.lang.python)
  • Re: BSD Sockets Question
    ... since Linux 2.4. ... and mark a socket readable when even a single ... there in the headers but they are noops in the runtime. ... I suspect that the works of Stevens, ...
    (comp.unix.programmer)
  • Re: Interest in AVR+MSP430 "general purpose" proto board?
    ... mount the SD socket on the back, rotated 180 degrees, so the SD ... All right, all right, what is with the CPLD? ... All the pins coming to headers, ...
    (comp.arch.embedded)
  • Re: How do I stat online files?
    ... I know how to stat a local file… ... Using an HTTP HEAD request would be better, as only the headers are transfered. ... Since urllib can't generate a HEAD request, one has to use httplib instead: ...
    (comp.lang.python)