Skip to content

Instantly share code, notes, and snippets.

@zvalentine22
Created September 14, 2020 06:31
Show Gist options
  • Save zvalentine22/50a6bc18dd051aae32bf0c2dd4ac27ee to your computer and use it in GitHub Desktop.
Save zvalentine22/50a6bc18dd051aae32bf0c2dd4ac27ee to your computer and use it in GitHub Desktop.
snapmaker2.0 upload without luban
import requests
import argparse
import os.path
import sys
debug = False
# if debug = True, ignore command line arguments and use the following:
ip = "192.168.0.47"
port = 8080
base_url = "http://{}:{}/api/v1".format(ip, port)
file = "test.gcode"
def get_token(url):
""" requests session token """
r = requests.post(url+"/connect")
if r.status_code == 200:
return r.json()["token"]
else:
return ""
def get_status(url, token):
""" request printer status """
payload = {"token": token}
r = requests.get(url+"/status", params=payload)
return r
def post_file(url, token, file):
""" upload file to printer """
payload = {"token": token}
file_payload = {"file": open(file, 'rb')}
r = requests.post(url+"/upload", params=payload, files=file_payload)
if __name__ == "__main__":
if not debug:
parser = argparse.ArgumentParser(description="upload a gcode file to snapmaker2.0")
parser.add_argument("-p", dest='port', help="port number (default=8080)", default=8080, type=int)
parser.add_argument("-i", dest='address', help="ip address or hostname of printer", default="127.0.0.1")
parser.add_argument("filename", help=".gcode file to upload", type=str)
args = parser.parse_args()
if not args.filename.endswith(".gcode"):
print("Invalid file type")
sys.exit()
file = args.filename
base_url = "http://{}:{}/api/v1".format(args.address, args.port)
try:
token = get_token(base_url)
assert token
except:
print("Couldn't connect to printer")
sys.exit()
status = get_status(base_url, token)
while status.status_code == 204:
status = get_status(base_url, token)
if status.status_code == 200:
try:
assert os.path.isfile(file)
print("Attempting upload...")
post_file(base_url, token, file)
except:
print("ERROR: check connection/path")
sys.exit()
else:
print("Upload rejected")
@halr9000
Copy link

Works well, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment