0

I am building a Rust program in which the user types in a command, and then the program reads the command and responds accordingly. One of these commands is to download a file from a set site.

I have a .py file with the following code that I made a while ago that downloads files from a set site:

import urllib
import urllib2
import requests

url = 'http://www.blog.pythonlibrary.org/wpcontent/uploads/2012/06/wxDbViewer.zip'

print "downloading with urllib"
urllib.urlretrieve(url, "code.zip")

print "downloading with urllib2"
f = urllib2.urlopen(url)
data = f.read()
with open("code2.zip", "wb") as code:
code.write(data)

print "downloading with requests"
r = requests.get(url)
with open("code3.zip", "wb") as code:
code.write(r.content)

The URLs in the code are not ones that I will be using; they are examples.

If the Rust program sets the site it needs to go to as a variable, is there a way that I could send the variable to my Python program? I know you can send Python to Rust:

Passing a list of strings from Python to Rust

http://www.joesacher.com/blog/2017/08/24/ptr-types/

Is there a way to do this in the other direction?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • You need something like https://github.com/lukemetz/rustpy or https://github.com/dgrunwald/rust-cpython – Paulo Scardine Jan 17 '18 at 20:40
  • 3
    1. You can save the urls in a file and make python load the urls from the file. 2. You can pass url as command line args to the python file. – Stack Jan 17 '18 at 20:41
  • I think I like the idea of the urls in a file, but then comes the problem of telling Python WHEN to download the files. Maybe the program reads the text file for a signal? –  Jan 17 '18 at 20:50
  • see also https://github.com/PyO3/pyo3 – user25064 Jan 17 '18 at 20:51
  • Stack, would the code I posted also function if instead of url = 'insert url here' I put url = x ? –  Jan 17 '18 at 20:59
  • 1
    It is really unclear what your desired architecture is. There are lots of ways to invoke Python code from Rust and vice versa (and in general, to invoke one program from another), including: 1) run Rust program as a subprocess from Python; 2) run Python program as a subprocess from Rust; 3) embed Python interpreter in a Rust program and run the script in it; 4) use the Rust program as a native module in a Python program; 5) run Rust and Python programs separately and use one of the zillion IPC mechanisms to communicate between them. You need to consider all these options and choose one. – Vladimir Matveev Jan 17 '18 at 23:23
  • 1
    It's not that clear to me why you need to use python in the first place. Your example program is just downloading three copies of the file using 3 different python libs. But if all you are trying to do is download a file why not do it directly in rust? – harmic Jan 18 '18 at 00:10

0 Answers0