Created
January 24, 2016 14:38
-
-
Save MStrecke/64bccbf447967da7337b to your computer and use it in GitHub Desktop.
Raw communication with owncloud server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import socket | |
import base64 | |
import urlparse | |
# Data for accessing the calendar | |
CALURL = 'http://hermitest/owncloud/remote.php/caldav/calendars/mike/testcal1/' | |
USERNAME = 'mike' | |
PASSWORD = 'XXXXXXXXX' | |
SOCKETLEN_WR = 256 | |
SOCKETLEN_RD = 2048 | |
class mysocket: | |
def __init__(self): | |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
def connect(self, host, port): | |
self.sock.connect((host, port)) | |
def mysend(self, msg): | |
MSGLEN = len(msg) | |
totalsent = 0 | |
while totalsent < MSGLEN: | |
sent = self.sock.send(msg[totalsent:totalsent + SOCKETLEN_WR]) | |
if sent == 0: | |
raise RuntimeError("socket connection broken") | |
totalsent = totalsent + sent | |
def myreceive(self): | |
# receive until socket is closed | |
chunks = [] | |
bytes_recd = 0 | |
while True: | |
chunk = self.sock.recv(SOCKETLEN_RD) | |
if chunk == '': | |
break | |
raise RuntimeError("socket connection broken") | |
chunks.append(chunk) | |
bytes_recd = bytes_recd + len(chunk) | |
return ''.join(chunks) | |
def create_msg(host, path, user, password,data): | |
authorization = base64.b64encode(user + ':' + password) | |
return """REPORT %s HTTP/1.1 | |
Host: %s | |
Content-Length: %s | |
Accept: text/xml | |
User-Agent: Mozilla/5.0 | |
Depth: 1 | |
Content-Type: application/xml; charset="utf-8" | |
Authorization: Basic %s | |
%s""" % ( | |
path, host, len(data), authorization, data | |
) | |
def send_data(host, port, data): | |
ms = mysocket() | |
ms.connect( host, port ) | |
ms.mysend(msg) | |
return ms.myreceive() | |
# Testdata to send | |
data_norm = """<?xml version='1.0' encoding='utf-8'?> | |
<C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav" xmlns:D="DAV:"> | |
<D:prop> | |
<C:calendar-data/> | |
</D:prop> | |
<C:filter> | |
<C:comp-filter name="VCALENDAR"> | |
<C:comp-filter name="VEVENT"> | |
<C:time-range start="20160101T000000Z"/> | |
</C:comp-filter> | |
</C:comp-filter> | |
</C:filter> | |
</C:calendar-query>""" | |
data_ext = """<?xml version='1.0' encoding='utf-8'?> | |
<C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav" xmlns:D="DAV:"> | |
<D:prop> | |
<C:calendar-data> | |
<C:expand start="20160101T000000Z" end="20160130T000000Z"/> | |
</C:calendar-data> | |
</D:prop> | |
<C:filter> | |
<C:comp-filter name="VCALENDAR"> | |
<C:comp-filter name="VEVENT"> | |
<C:time-range start="20160101T000000Z" end="20160130T000000Z"/> | |
</C:comp-filter> | |
</C:comp-filter> | |
</C:filter> | |
</C:calendar-query>""" | |
# split URL | |
urlparts = urlparse.urlparse(CALURL) | |
assert urlparts.scheme == 'http' | |
url_path = urlparts.path | |
host_port = urlparts.netloc # myhost or myhost:port | |
p = host_port.find(':') | |
if p == -1: | |
host = host_port | |
port = 80 | |
else: | |
host = host_port[:p] | |
port = int(host_port[(p + 1):]) | |
msg = create_msg(host_port, url_path, USERNAME, PASSWORD, data_norm) | |
print(msg) | |
res = send_data(host, port, msg) | |
print('------------------------------------------') | |
print(res) | |
print('==========================================') | |
msg = create_msg(host_port, url_path, USERNAME, PASSWORD, data_ext) | |
print(msg) | |
res = send_data(host, port, msg) | |
print('------------------------------------------') | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a small program to demonstrate an error occurring in the calendar app of owncloud 8.1 and 8.2 when trying to select a date range.
See also owncloud/calendar#1013