简体   繁体   中英

How can I host a python server script, on a cloud hosted server (Google, Amazon, Azure...)?

let me start by answering the obvious. Why would I want to do this? Actually, I don't. I made the following program below, but it's designed to run on a remote server. I'm basically using the socket library, but I want to host it on another machine, preferbly a google, amazon, azure, etc. But, as I knew before I tried, this was slightly not possible. Google app engine gave me an error, like "access denied to socket blah blah blah".

I feel I'm left with 2 options:

I can continue to run this code on my own servers, if I can figure out how to host this server script on a hosted cloud base server, or I could take the code, every bit that doesn't contain the server portion, and make it get the "data" from the clients, via POST requests.

the data is what's sent from the client...

bap = {}
while 1:
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_address = ("localhost", 8081)
    #print 'starting up on %s port %s' % server_address
    server.bind(server_address)
    server.listen(5)
    connection, client_address = server.accept()
    #print 'connection from', connection.getpeername()
    server.close()
    data = connection.recv(4096)

    if data in bap:
        print data + " is checking in!!!"
        for k, v in bap.iteritems():
            if k == data:
                bap[k] = 10
                print bap
                c = open('check.json', "w")
                wiz = json.dumps(bap)
                c.write(wiz)
                c.close()
            else:
                bap[k] -= 1
                if bap[k] < 0:
                    print k + " is Offline!!!"
                    mail()
                    c = open('log.txt', "a")
                    wiz = json.dumps(bap)
                    time1 = datetime.datetime.now().strftime("%m/%d/%y %H:%M ")
                    c.write(k + " is offline!!! "+ time1 + "\n")
                    c.close()
                else:
                    print bap
    else:
        bap[data] = 10
        print data + " was added!!!"

Running a python script listening to external ports can be done on Amazon EC2.

1) Create an EC2 Instance using the Management Console at Amazon Web Services .

2) Edit the Security Group associated with your instance so it opens the port number you want your python script to be listening to.

3) Upload and run your script on the EC2 instance. Be sure the port number that your script is listening on is the same as the one you opened in the Security Group.

If you SSH into your EC2 instance, you may want to run the python script in a "background process" using something like TMUX . Else when you terminate the SSH connection your python script will stop running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM