Get file from PLC to PC with Python

Hello,

I am trying to get .bin files from PLC to PC. Currently I can do so with usage of FB IL_FTPPutFileAsync (from CXA_SocketComm library). Which means initiate data transfer from PLC (client) to PC (server).

But my question is, is it possible to initiate data transfer from PC (specifically with Python)? Should it be possible with FTP or with WebDAV e.g.?

I have read a lot of similar topics so sorry for asking question again, but I haven't found clear answer for initiating that kind of data transfer from PC.

Thanks a lot for your support.

Best reply by AndreasSwe

Do get a file from a core to a PC, you can just use the REST interface like this:

import urllib.request
import ssl
import json

ssl._create_default_https_context = ssl._create_unverified_context

def auth():
    head = {
        "Content-Type": "application/json",
        "Accept": "application/json",
    }

    body = {
        'name': 'boschrexroth',
        'password': 'boschrexroth',
    }

    data = json.dumps(body).encode("utf-8")

    req = urllib.request.Request('https://192.168.1.1/identity-manager/api/v2/auth/token', data, head)

    with urllib.request.urlopen(req) as f:
        res = f.read()

    resonse = json.loads(res.decode())

    return resonse['access_token']

 

def download(token, name):
    opener = urllib.request.build_opener()
    opener.addheaders = [('Authorization', 'Bearer ' + token)]

    urllib.request.install_opener(opener)
    urllib.request.urlretrieve("https://192.168.1.1/solutions/files/DefaultSolution/configurations/appdata/" + name, name)
    

token = auth()

download(token, 'configuration.json')

Modify the IP-adress and filenames according to your settings.

This can download files from the APP data folders of the core, in the example above the configuration.json file:

 

View original
3 replies