Auto-update Ubuntu IP address in python script










0















I am using python3 , flask server on ubuntu virtual machine. The server is working fine. It uploads a browsed video and print **found file when the browsed video file is in appropriate extension.



The problem is, every time when I restart my ubuntu virtual machine the IP address is changed and I have to update it manually in python script app.run(host= '192.168.1.101', debug=True).



Please tell me how to automatically update it.



Any help would be appreciated.
Thanks in advance.



Here is the code.



import os
from flask import Flask, request, redirect, url_for, send_from_directory, jsonify
from werkzeug import secure_filename

UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'mp4'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
# this has changed from the original example because the original did not work for me
return filename[-3:].lower() in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
print("Printing the request ".format(request.headers))
print("Printing the request ".format(request))
print("Printing the request ".format(request.data))
print("Printing the request ".format(request.form))
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
print('**found file', file.filename)
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# for browser, add 'redirect' function on top of 'url_for'
return jsonify("success" : url_for('uploaded_file',
filename=filename))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''

@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)

if __name__ == '__main__':
app.run(host= '192.168.1.101', debug=True)









share|improve this question


























    0















    I am using python3 , flask server on ubuntu virtual machine. The server is working fine. It uploads a browsed video and print **found file when the browsed video file is in appropriate extension.



    The problem is, every time when I restart my ubuntu virtual machine the IP address is changed and I have to update it manually in python script app.run(host= '192.168.1.101', debug=True).



    Please tell me how to automatically update it.



    Any help would be appreciated.
    Thanks in advance.



    Here is the code.



    import os
    from flask import Flask, request, redirect, url_for, send_from_directory, jsonify
    from werkzeug import secure_filename

    UPLOAD_FOLDER = 'uploads'
    ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'mp4'])

    app = Flask(__name__)
    app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

    def allowed_file(filename):
    # this has changed from the original example because the original did not work for me
    return filename[-3:].lower() in ALLOWED_EXTENSIONS

    @app.route('/', methods=['GET', 'POST'])
    def upload_file():
    print("Printing the request ".format(request.headers))
    print("Printing the request ".format(request))
    print("Printing the request ".format(request.data))
    print("Printing the request ".format(request.form))
    if request.method == 'POST':
    file = request.files['file']
    if file and allowed_file(file.filename):
    print('**found file', file.filename)
    filename = secure_filename(file.filename)
    file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    # for browser, add 'redirect' function on top of 'url_for'
    return jsonify("success" : url_for('uploaded_file',
    filename=filename))
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
    <p><input type=file name=file>
    <input type=submit value=Upload>
    </form>
    '''

    @app.route('/uploads/<filename>')
    def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
    filename)

    if __name__ == '__main__':
    app.run(host= '192.168.1.101', debug=True)









    share|improve this question
























      0












      0








      0








      I am using python3 , flask server on ubuntu virtual machine. The server is working fine. It uploads a browsed video and print **found file when the browsed video file is in appropriate extension.



      The problem is, every time when I restart my ubuntu virtual machine the IP address is changed and I have to update it manually in python script app.run(host= '192.168.1.101', debug=True).



      Please tell me how to automatically update it.



      Any help would be appreciated.
      Thanks in advance.



      Here is the code.



      import os
      from flask import Flask, request, redirect, url_for, send_from_directory, jsonify
      from werkzeug import secure_filename

      UPLOAD_FOLDER = 'uploads'
      ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'mp4'])

      app = Flask(__name__)
      app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

      def allowed_file(filename):
      # this has changed from the original example because the original did not work for me
      return filename[-3:].lower() in ALLOWED_EXTENSIONS

      @app.route('/', methods=['GET', 'POST'])
      def upload_file():
      print("Printing the request ".format(request.headers))
      print("Printing the request ".format(request))
      print("Printing the request ".format(request.data))
      print("Printing the request ".format(request.form))
      if request.method == 'POST':
      file = request.files['file']
      if file and allowed_file(file.filename):
      print('**found file', file.filename)
      filename = secure_filename(file.filename)
      file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
      # for browser, add 'redirect' function on top of 'url_for'
      return jsonify("success" : url_for('uploaded_file',
      filename=filename))
      return '''
      <!doctype html>
      <title>Upload new File</title>
      <h1>Upload new File</h1>
      <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
      <input type=submit value=Upload>
      </form>
      '''

      @app.route('/uploads/<filename>')
      def uploaded_file(filename):
      return send_from_directory(app.config['UPLOAD_FOLDER'],
      filename)

      if __name__ == '__main__':
      app.run(host= '192.168.1.101', debug=True)









      share|improve this question














      I am using python3 , flask server on ubuntu virtual machine. The server is working fine. It uploads a browsed video and print **found file when the browsed video file is in appropriate extension.



      The problem is, every time when I restart my ubuntu virtual machine the IP address is changed and I have to update it manually in python script app.run(host= '192.168.1.101', debug=True).



      Please tell me how to automatically update it.



      Any help would be appreciated.
      Thanks in advance.



      Here is the code.



      import os
      from flask import Flask, request, redirect, url_for, send_from_directory, jsonify
      from werkzeug import secure_filename

      UPLOAD_FOLDER = 'uploads'
      ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'mp4'])

      app = Flask(__name__)
      app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

      def allowed_file(filename):
      # this has changed from the original example because the original did not work for me
      return filename[-3:].lower() in ALLOWED_EXTENSIONS

      @app.route('/', methods=['GET', 'POST'])
      def upload_file():
      print("Printing the request ".format(request.headers))
      print("Printing the request ".format(request))
      print("Printing the request ".format(request.data))
      print("Printing the request ".format(request.form))
      if request.method == 'POST':
      file = request.files['file']
      if file and allowed_file(file.filename):
      print('**found file', file.filename)
      filename = secure_filename(file.filename)
      file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
      # for browser, add 'redirect' function on top of 'url_for'
      return jsonify("success" : url_for('uploaded_file',
      filename=filename))
      return '''
      <!doctype html>
      <title>Upload new File</title>
      <h1>Upload new File</h1>
      <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
      <input type=submit value=Upload>
      </form>
      '''

      @app.route('/uploads/<filename>')
      def uploaded_file(filename):
      return send_from_directory(app.config['UPLOAD_FOLDER'],
      filename)

      if __name__ == '__main__':
      app.run(host= '192.168.1.101', debug=True)






      python-3.x flask ubuntu-14.04






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 15:52









      Afshan AnwaraliAfshan Anwarali

      206




      206






















          1 Answer
          1






          active

          oldest

          votes


















          0














          you will have to either parse the "ip address" or "ifconfig" command, or use



          import socket
          socket.gethostbyname(socket.gethostname())


          cfr. Finding local IP addresses using Python's stdlib






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265698%2fauto-update-ubuntu-ip-address-in-python-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            you will have to either parse the "ip address" or "ifconfig" command, or use



            import socket
            socket.gethostbyname(socket.gethostname())


            cfr. Finding local IP addresses using Python's stdlib






            share|improve this answer



























              0














              you will have to either parse the "ip address" or "ifconfig" command, or use



              import socket
              socket.gethostbyname(socket.gethostname())


              cfr. Finding local IP addresses using Python's stdlib






              share|improve this answer

























                0












                0








                0







                you will have to either parse the "ip address" or "ifconfig" command, or use



                import socket
                socket.gethostbyname(socket.gethostname())


                cfr. Finding local IP addresses using Python's stdlib






                share|improve this answer













                you will have to either parse the "ip address" or "ifconfig" command, or use



                import socket
                socket.gethostbyname(socket.gethostname())


                cfr. Finding local IP addresses using Python's stdlib







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 '18 at 16:01









                TohmaxxxTohmaxxx

                1766




                1766



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265698%2fauto-update-ubuntu-ip-address-in-python-script%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo