Home>

The question is that I want to write a web server in Django, and there I need to build a parser in Java, after which I need to process the received data in Python. How to cross it all? Will Jython work?

Just run Java parser in a separate process and exchange data via stdin /stdout for example?

andreymal2021-04-18 02:30:37

Jython is fine for me.

Victor VosMottor2021-04-18 02:30:37

@VictorVosMottor, I personally would not consider such an option until the 3rd version of Python is implemented in Jython.

insolor2021-04-18 02:30:37

You can make the parser a separate (micro) service, exchange data via HTTP.

insolor2021-04-18 02:30:37

@insolor i.e. now jython only works with python 2?

user3921702021-04-18 02:30:37
  • Answer # 1

    pip install jpype1 -U

    import jpype
    import jpype.imports
    from jpype.types import *
    def start_JVM ():
        if jpype.isJVMStarted ():
            return
        else:
            jpype.startJVM ()
    start_JVM ()
    from java.lang import System
    def main ():
        System.out.println ("Hello Java!"); # ->
     Hello Java!
    if __name__== '__main__':
        main ()
    

    A more complex example:

    import jpype
    import jpype.imports
    from jpype.types import *
    def start_JVM ():
        jpype.isJVMStarted ():
            return
        else:
            jpype.startJVM ()
    start_JVM ()
    from java.awt import Dimension
    from javax.swing import JFrame
    from javax.swing import JLabel
    class TestAWT:
        def __init __ (self):
            frame= JFrame ("Test AWT")
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)
            label= JLabel ("Hello from Java!")
            frame.getContentPane (). add (label)
            frame.setPreferredSize (Dimension (250, 100))
            frame.pack ()
            frame.setVisible (True)
    def main ():
        TestAWT ()
    if __name__== '__main__':
        main ()
    

    I don't understand why write a parser in Java ;-)

    import os
    import jpype
    import jpype.imports
    from jpype.types import *
    def start_JVM ():
        if jpype.isJVMStarted ():
            print ('JVM started earlier ...')
            return
        else:
            print ('Start JVM')
            jpype.startJVM (jpype.getDefaultJVMPath (), "-ea", "-
        Djava.class.path= jsoup-1.13.1.jar ")
    def main ():
        start_JVM ()
        JSoup= jpype.JPackage ("org"). Jsoup.Jsoup
        document= JSoup.connect ("https://yandex.ru/")
        document.userAgent ("Chrome /4.0.249.0 Safari /532.5")
        document.referrer ("http://www.google.com")
        print (document.get ())
    if __name__== '__main__':
        main ()
    

    JPype Documentation

    Not my parser, unfortunately. They said to use it.

    user3921702021-04-18 02:45:01

    Well, use it I showed you how)

    Александр2021-04-18 02:45:01

    If you liked the answer, you can express this by clicking on the corresponding button)

    Александр2021-04-18 02:45:01
  • Answer # 2

    pip install jpype1 -U

    import jpype
    import jpype.imports
    from jpype.types import *
    def start_JVM ():
        if jpype.isJVMStarted ():
            return
        else:
            jpype.startJVM ()
    start_JVM ()
    from java.lang import System
    def main ():
        System.out.println ("Hello Java!"); # ->
     Hello Java!
    if __name__== '__main__':
        main ()
    

    A more complex example:

    import jpype
    import jpype.imports
    from jpype.types import *
    def start_JVM ():
        jpype.isJVMStarted ():
            return
        else:
            jpype.startJVM ()
    start_JVM ()
    from java.awt import Dimension
    from javax.swing import JFrame
    from javax.swing import JLabel
    class TestAWT:
        def __init __ (self):
            frame= JFrame ("Test AWT")
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)
            label= JLabel ("Hello from Java!")
            frame.getContentPane (). add (label)
            frame.setPreferredSize (Dimension (250, 100))
            frame.pack ()
            frame.setVisible (True)
    def main ():
        TestAWT ()
    if __name__== '__main__':
        main ()
    

    I don't understand why write a parser in Java ;-)

    import os
    import jpype
    import jpype.imports
    from jpype.types import *
    def start_JVM ():
        if jpype.isJVMStarted ():
            print ('JVM started earlier ...')
            return
        else:
            print ('Start JVM')
            jpype.startJVM (jpype.getDefaultJVMPath (), "-ea", "-
        Djava.class.path= jsoup-1.13.1.jar ")
    def main ():
        start_JVM ()
        JSoup= jpype.JPackage ("org"). Jsoup.Jsoup
        document= JSoup.connect ("https://yandex.ru/")
        document.userAgent ("Chrome /4.0.249.0 Safari /532.5")
        document.referrer ("http://www.google.com")
        print (document.get ())
    if __name__== '__main__':
        main ()
    

    JPype Documentation

    Not my parser, unfortunately. They said to use it.

    user3921702021-04-18 02:45:01

    Well, use it I showed you how)

    Александр2021-04-18 02:45:01

    If you liked the answer, you can express this by clicking on the corresponding button)

    Александр2021-04-18 02:45:01