Git Product home page Git Product logo

f2py-sockets's Introduction

F2Py Sockets

A minimal working example on how to use sockets to transfer data from Fortran to Python and viceversa

This repository shows how to pass a n-dimensional array from Fortran to Python and viceversa. The Fortran fsockets.f90 (wrapper of the sockets.c), the driver.f90 and the socket.py files were found in the i-pi repository (see for further functionalities). Here the driver.f90 and the sockets.py files have been modified to show a simple example on how to easily transfer data.

How it works

A socket is created in Python allowing remote connections on a certain port and address

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 	# Creates INET socket with TCP protocol
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)   # Set socket options
server.bind((self.address, self.port))	# What addresses and port does the socket listen to
server.listen(1) # How many connection requests do we allow at the same time

Now a socket has been opened by the server and can be contacted by the remote socket, our driver, a.k.a client (driver.f90). The remote socket needs to be opened in Fortran using the fsockets.f90 wrapper of the sockets.c. We only need to use the wrapper as follows

call open_socket(socket, inet, port, host)

where socket is the socket id, inet is the TCP protocol specification, port is the port number and host is the server address.

From the server (Python) side, we need to accept the Fortran connection. For example,

while True:
    # accept connections from outside
    (client, address) = server.accept()
    # now do something with the clientsocket, for example send a message
    client.sendall("A message!".encode())

Requirements

python3.x and numpy required. For older Python versions (2.x) check the python_2.7 branch. The main difference is found in the creation of a socket object from another socket object.

Fortran and C compilers also required. Works fine with gfortran and gcc respectively. The Fortran compiler choice is specifically important for the declaration of single/double precision variables in Fortran for which the (e.g.) real(kind=4) standard is used instead of real*4.

Usage

Run the Python server with

cd python
python main.py

The Fortran driver needs to be compiled first:

cd ../fortran
make

And now you can run it with

./driver.x -h localhost -p 31415

The driver should now be able to find the server socket. And the data will start transfering from Fortran to Python and viceversa.

f2py-sockets's People

Contributors

b-fg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.