I have two computers on a network. I'll call the computer I'm working on computer A and the remote computer B. My goal is to send a command to B, gather some information, transfer this information to computer A and use this in some meaningfull way. My current method follows:
Establish a connection to B with paramiko.
Use paramiko to execute a remote command e.g. paramiko.exec_command('python file.py').
Write the information to a file with pickle, and use paramiko.ftp to transfer the file to computer A.
Open this file and parse the information back into a usable form, like a class or dictionary.
This seems very ad-hoc. My question is, is there a better way to transfer data between computers using Python? I know how to transfer files, this is a different question. I want to make an object on B and use it on A.
I would recommend taking a look at twisted. It's the best python framework I've encountered for custom client/server applications like the one you describe.
I have been doing something similar on a larger scale (with 15 clients). I use the pexpect module to do essentially ssh commands on the remote machine (computer B). Another module to look into would be the subprocess module.
We are developing Versile Python which provides such object-level interaction, you may want to have a look. It includes an object-level framework for streams which could be applied to your use case (transferring file data).
Related
I have two Python programs, one is a IRC bot, using sockets to connect to an IRC server.
This program has a loop that reads every PRIVMSG from an specific channel.
The second program should get whatever the first program outputs (the PRIVMSG in this case) and runs functions with it.
So, it's basically:
while 1:
data = irc.recv(2048)
if data.find("PRIVMSG " + current_channel + " :") != -1:
send_to_second_program(data)
the second program is
while 1:
data = get_from_first_program()
do_stuff(data)
Is there a way to make this happen without using modules? The two programs should be separate.
Although you could use literally dozens of ways to communicate and you provide no context or requirements, I assume that you are working on a small project. I would recommend you to use rpc (remote procedure call). Using rpc you can call Python functions of another application as if it is a function available locally. Check out some rpc library for Python, like http://www.zerorpc.io/, which seems more than enough for your use case.
There are some downsides to using rpc, which you can read about in the answer of this question for example, but if the scope is limited I think this is one of easiest, non-hack, ways to achieve your goal.
Assuming that both programs are in the same directory, you can import the other file using import
For example, consider a file a.py. To import it in another file b.py use import a Then use a.something to access something defined in file a.py
Over JSON (Rest API). It's service oriented architecture. So, you can very scale your application.
Over Message queue It's scaleable too, but it's not very reliability.
With Python (I am using v3.4), I am creating a program that will run on up to 20 machines concurrently connected to a same network.
Those machines will need from time to time to access the network and make some operations (create/delete/modify files)…
What I had currently planned was:
network will have a folder "db"
I will use the shelve module to have my shared data (a simple dictionary of less than 3,000 entries containing some path names and some logging information) in this folder. Since there is not so much data it is a convenient module that will be fast enough
to avoid operations by multiple machines on the network at the same time (and avoid conflicts), machines will have to lock the shelve file on the network before doing anything (anyway they will also need to update this file) and will unlock only at the end of their operation (updating its contents if necessary)
The problem I have is that shelve does not have a convenient way for concurrent access or locking mechanism. I understand that my 2 possibilities are:
either I don't use shelve and use another module to manage my simple database (ex: sqlite3 except it is a bit more complex than the simple shelve module)
either I create a locking mechanism that will work on network by multiple machines (so far I didn't find a module that seemed totally reliable)
Additional requirements (if possible) are:
It will mainly be used with Windows but I would like the solution to be cross-platform so that I can re-use it with Linux
network file system will be anything accessible through a standard explorer (in linux/windows) through an address like "\Machine\Folder". It could be either a file server or just a shared folder present on one of the machines.
Any recommendation?
Thanks
Multiplatform, locking. You best option is portalocker (https://pypi.python.org/pypi/portalocker/0.3). We've used it extensively -- and it's a winner !
I have to setup a program which reads in some parameters from a widget/gui, calculates some stuff based on database values and the input, and finally sends some ascii files via ftp to remote servers.
In general, I would suggest a python program to do the tasks. Write a Qt widget as a gui (interactively changing views, putting numbers into tables, setting up check boxes, switching between various layers - never done something as complex in python, but some experience in IDL with event handling etc), set up data classes that have unctions, both to create the ascii files with the given convention, and to send the files via ftp to some remote server.
However, since my company is a bunch of Windows users, each sitting at their personal desktop, installing python and all necessary libraries on each individual machine would be a pain in the ass.
In addition, in a future version the program is supposed to become smart and do some optimization 24/7. Therefore, it makes sense to put it to a server. As I personally rather use Linux, the server is already set up using Ubuntu server.
The idea is now to run my application on the server. But how can the users access and control the program?
The easiest way for everybody to access something like a common control panel would be a browser I guess. I have to make sure only one person at a time is sending signals to the same units at a time, but that should be doable via flags in the database.
After some google-ing, next to QtWebKit, django seems to the first choice for such a task. But...
Can I run a full fledged python program underneath my web application? Is django the right tool to do so?
As mentioned previously, in the (intermediate) future ( ~1 year), we might have to implement some computational expensive tasks. Is it then also possible to utilize C as it is within normal python?
Another question I have is on the development. In order to become productive, we have to advance in small steps. Can I first create regular python classes, which later on can be imported to my web application? (Same question applies for widgets / QT?)
Finally: Is there a better way to go? Any standards, any references?
Django is a good candidate for the website, however:
It is not a good idea to run heavy functionality from a website. it should happen in a separate process.
All functions should be asynchronous, I.E. You should never wait for something to complete.
I would personally recommend writing a separate process with a message queue and the website would only ask that process for statuses and always display a result immediatly to the user
You can use ajax so that the browser will always have the latest result.
ZeroMQ or Celery are useful for implementing the functionality.
You can implement functionality in C pretty easily. I recomment however that you write that functionality as pure c with a SWIG wrapper rather that writing it as an extension module for python. That way the functionality will be portable and not dependent on the python website.
So I have tried to find a answer but must not be searching correctly or what I'm trying to do is the wrong way to go about it.
So I have a simple python script that creates a chess board and pieces in a command line environment. You can in put commands to move the pieces. So one of my co workers thought it would be cool to play each other over the network. I agreed and tried by creating a text file to read and write to on the network share. Then we would both run the script that reads that file. The problem I ran into is I pretty much DOS attacked that file share since it kept trying to check that file on network share for a update.
I am still new to python and haven't ever wrote code that travels the internet, our even a simple local network. So my question is how should I go about properly allowing 2 people to access this data at the same time with out stealing all the network resources?
Oh also im using version 2.6 because thats what everyone else uses and they refuse to change to new syntax
You need to use the proper networking way. It's not quite hard for simple networked program like yours.
Use the one from the Python's stdlib http://docs.python.org/library/socket.html (also take a look at the examples at the bottom of the page).
First off, without knowing how many times you are checking the fle with the moves, it is difficult to know why the file-share is getting DoS-ed. Most networks and network shares these days can handle that level of traffic - they are all gigabit Ethernet, so unless you are transferring large chunks of data each time, you should be ok. If you are transferring the whole file each time, then I'd suggest that you look at optimizing that.
That said, coming to your second question on how this is handled at a network level, to be honest, you are already doing it in a certain way - you are accessing a file on a network share and modifying it. The only optimization required is to be able to do it efficiently. Even over the network operations in a concurrent world do the same. In that case, it will be using fast in-memory database storing various changes / using a high-scale RDBMS / in the case of fast-serving web-servers better async I/O.
In the current case, since there are two users playing the game, I suggest that you work on a way to transmit only the difference in the moves each time over the network. So, instead of modifying the file over the network share, you can send the moves over to a server component and it synchronizing the changes locally to the file. Of course, this means you will need to create a server component that would do something like this
user1's moves <--> server <--> user2's moves . Server will modify the moves file.
Once you start doing this, you get into the realm of server programming / preventing race conditions etc. It will be a good learning experience.
I want to program a virtual file system in Windows with Python.
That is, a program in Python whose interface is actually an "explorer windows". You can create & manipulate file-like objects but instead of being created in the hard disk as regular files they are managed by my program and, say, stored remotely, or encrypted or compressed or versioned, or whatever I can do with Python.
What is the easiest way to do that?
While perhaps not quite ripe yet (unfortunately I have no first-hand experience with it), pywinfuse looks exactly like what you're looking for.
Does it need to be Windows-native? There is at least one protocol which can be both browsed by Windows Explorer, and served by free Python libraries: FTP. Stick your program behind pyftpdlib and you're done.
Have a look at Dokan a User mode filesystem for Windows. There are Ruby, .NET (and Java by 3rd party) bindings available, and I don't think it'll be difficult to write python bindings either.
If you are trying to write a virtual file system (I may misunderstand you) - I would look at a container file format. VHD is well documented along with HDI and (embedded) OSQ. There are basically two things you need to do. One is you need to decide on a file/container format. After that it is as simple as writing the API to manipulate that container. If you would like it to be manipulated over the internet, pick a transport protocol then just write a service (would would emulate a file system driver) that listens on a certain port and manipulates this container using your API
You might be interested in PyFilesystem;
A filesystem abstraction layer for Python
PyFilesystem is an abstraction layer for filesystems. In the same way that Python's file-like objects provide a common way of accessing files, PyFilesystem provides a common way of accessing entire filesystems. You can write platform-independent code to work with local files, that also works with any of the supported filesystems (zip, ftp, S3 etc.).
What the description on the homepage does not advertise is that you can then expose this abstraction again as a filesystem, among others SFTP, FTP (though currently disfunct, probably fixable) and dokan (dito) as well as fuse.