使用Python

时间:2016-08-28 18:33:17

标签: python api

我正在开发一个用Python编写的项目,需要将更新发布到Friendica服务器并使用各种可用的API进行交互。但是,我的API使用经验非常有限,因此我不确定如何在Python中编写此功能。在Friendica GitHub上有一个例子,但Python示例对我不起作用。有一个Python3模块https://bitbucket.org/tobiasd/python-friendica/overview,但是在尝试使用它在测试脚本中进行连接时,如下所示:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import friendica
# make a new instance of friendica
f = friendica.friendica (server = '10.211.55.23/api/statuses/update', username = 'newtest', password = 'klaup8744')

# check that we are logged in
f.account_verify_credentials()

# get the current notifications
print (f.ping())

# post something with the default settings
f.statuses_update( status = "here is the message you are going to post" )

它会拒绝与以下消息的连接:

Traceback (most recent call last):
  File "/usr/lib/python3.4/urllib/request.py", line 1182, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/usr/lib/python3.4/http/client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.4/http/client.py", line 1126, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.4/http/client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.4/http/client.py", line 922, in _send_output
    self.send(msg)
  File "/usr/lib/python3.4/http/client.py", line 857, in send
    self.connect()
  File "/usr/lib/python3.4/http/client.py", line 1223, in connect
    super().connect()
  File "/usr/lib/python3.4/http/client.py", line 834, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python3.4/socket.py", line 512, in create_connection
    raise err
  File "/usr/lib/python3.4/socket.py", line 503, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test_1.py", line 12, in <module>
    print (f.ping())
  File "/home/sambraidley/Desktop/friendica.py", line 851, in ping
    res = urlopen(self.protocol()+self.apipath[:-4]+'/ping').read().decode('utf-8')
  File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 463, in open
    response = self._open(req, data)
  File "/usr/lib/python3.4/urllib/request.py", line 481, in _open
    '_open', req)
  File "/usr/lib/python3.4/urllib/request.py", line 441, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 1225, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.4/urllib/request.py", line 1184, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 111] Connection refused>

可以在以下https://bitbucket.org/tobiasd/python-friendica/src/b0b75ae80a6e747e8724b1ae36972ebfd939beb5/friendica.py?fileviewer=file-view-default

找到friendica Python3模块

我的Friendica服务器是在虚拟机中设置的,地址为10.211.55.23,测试凭据为username ='newtest',密码为'klaup8744',它完全正常工作,使用curl示例代码发布更新工作完美,如下:

/usr/bin/curl -u newtest:klaup8744 10.211.55.23/api/statuses/update.xml -d source="Testing" -d status="This is a test status"

1 个答案:

答案 0 :(得分:0)

根据您发布的source link,friendica默认使用https。您成功的卷曲请求是使用http。
尝试使用http:

实例化friendica
having