买阿里云

86 篇文章 3 订阅
16 篇文章 0 订阅

腾讯云99元/年的到期了。看到阿里云个人版有87元/年的版本,买了。

  • 购买配置阿里云

 

  • 支付宝扫描登陆控制台

  •  选“云服务器ESC"

  • 选”实例“——>”远程连接”——“更多”——>“密码”——>“重置实例密码”

  •  设置登陆密码

  • 打开WINDOWS远程桌面——>点击“显示选项”

  • 输入云公网地址,用户名Administrator,点击连接

  • 输入Administrator对应的密码,就是上面自己重置的密码

  • 远程连接成功

  • 然后就可以布置自己的服务器了。

右键桌面——>个性化设置——>主题——>桌面图标设置。吧桌面几个图标都显示

  • 安装配置软件

  • 安装JDK、python37、apache-tomecat、mysql

  • 添加JAVA_HOME、MYSQL_HOME环境变量,并且加到Path里
  • 安装pymysql、flask插件

pip install  pymysql  -i http://pypi.douban.com/simple/  --trusted-host pypi.douban.com
pip install  flask  -i http://pypi.douban.com/simple/  --trusted-host pypi.douban.com
  • 设置MYSQL环境

  •    先用services.msc看看服务器是否有mysql服务
  • 没有的话就安装
C:\program\mysql-8.0.18-winx64\bin>mysqld -install
Service successfully installed.
  •  再次看服务列表。寻找包含mysql字段的服务,设置为自动

  • 开机自启动tomcat

  • apache-tomecat\bin中安装
C:\program\apache-tomcat-9.0.33\bin>service.bat install
Installing the service 'Tomcat9' ...
Using CATALINA_HOME:    "C:\program\apache-tomcat-9.0.33"
Using CATALINA_BASE:    "C:\program\apache-tomcat-9.0.33"
Using JAVA_HOME:        "C:\program\Java\jdk1.8.0_131"
Using JRE_HOME:         "C:\program\Java\jdk1.8.0_131\jre"
Using JVM:              "C:\program\Java\jdk1.8.0_131\jre\bin\server\jvm.dll"
The service 'Tomcat9' has been installed.
  •  任务管理器——>服务——>打开服务。把apache tomecat改成自动

 

  • 测试服务器代码

  • 然后把自己的服务器布置上去试试
# coding=utf-8

import select
import socket
import threading
from logic.c_msg import CMsg
from logic.c_send import CSend


class CSelect(threading.Thread):

    def __init__(self, lock, users):
        threading.Thread.__init__(self)
        self.mLock = lock
        self.mUsers = users

    def run(self):
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server.setblocking(False)
        host = socket.gethostname()
        print("host = {}".format(host))
        server.bind(("0.0.0.0", 9090))
        server.listen(5)

        inputs = [server]
        outputs = []
        uid = 0

        while True:
            rlist, wlist, elist = select.select(inputs, outputs, inputs)
            #NOTE: 没有客户端连接时,一直锁定在此。一旦有客户端连接上了,就一直循环检测,不停打印下面的语句
            # print("len(rlist)={}, len(wlist)={}, len(elist)={}".format(len(rlist), len(wlist), len(elist)))
            # 遍历可读
            for r in rlist:
                if r is server:
                    conn, addr = r.accept()
                    conn.setblocking(False)
                    inputs.append(conn)
                    outputs.append(conn)
                    uid += 1
                    self.mUsers[conn] = CMsg(conn, addr, uid)
                    print("accept: {},{}".format(uid, conn))
                else:

                    try:
                        data = r.recv(1024)
                        if (data):
                            self.mLock.acquire()
                            txt = data.decode("utf-8")
                            print("recv: txt={}, r={} ".format(txt, r))
                            user = self.mUsers[r]
                            content = "[用户({})]:{}".format(user.mUid, txt)
                            user.addContent(content)
                            self.mLock.release()

                        else:  # 数据为空,则是客户端断开
                            print("断开 {}".format(r))
                            if r in outputs:
                                outputs.remove(r)
                            inputs.remove(r)
                            r.close()
                            self.mLock.acquire()
                            self.mUsers[r].reset()
                            del self.mUsers[r]
                            self.mLock.release()

                    except ConnectionResetError as e:
                        print("用户断开:r={},  e={}".format(r, e))
                        if r in outputs:
                            outputs.remove(r)
                        inputs.remove(r)
                        r.close()

                        self.mLock.acquire()
                        self.mUsers[r].reset()
                        del self.mUsers[r]
                        self.mLock.release()
                    finally:
                        pass

            self.mLock.acquire()
            # 遍历可写
            for conn, msg in self.mUsers.items():
                msg.mIsConnected = False

            for w in wlist:
                if w in self.mUsers:
                    self.mUsers[w].mIsConnected = True

            for s in elist:
                if (s in self.mUsers):
                    self.mUsers[s].reset()
                    del self.mUsers[s]

                (s in inputs) and inputs.remove(s)
                (s in outputs) and outputs.remove(s)
                s.close()
            self.mLock.release()


if __name__ == "__main__":
    lock = threading.Lock()
    users = {}
    sel = CSelect(lock, users)
    sel.start()

    send = CSend(lock, users)
    send.start()

    while True:
        line = input("输入:")
        if line == 'q':
            break

c_msg.py

# coding=utf-8

from queue import Queue


class CMsg():

    def __init__(self, so, addr, uid=0):
        self.mSock = so
        self.mAddr = addr
        self.mContentQu = Queue()
        self.mUid = uid
        self.mIsConnected = True

    def addContent(self, content):
        self.mContentQu.put(content)

    def reset(self):
        self.mContentQu.queue.clear()
        self.mUid = 0
        self.mIsConnected = True

c_send.py

# coding=utf-8

import threading
import time


class CSend(threading.Thread):

    def __init__(self, lock, allUsers):
        threading.Thread.__init__(self)
        self.mLock = lock
        self.mAllUsers = allUsers

    def run(self):
        print("CSend:: start")
        while(True):
            self.mLock.acquire()
            activeUsers = {}

            for conn, user in self.mAllUsers.items():
                if not user.mContentQu.empty():
                    activeUsers[conn] = user

            if activeUsers:
                for aconn, auser in activeUsers.items():
                    sz = auser.mContentQu.qsize()
                    for i in range(sz):
                        data = auser.mContentQu.get()
                        # print("CSend::run {}, {}".format(auser.mUid, data))

                        for ccon, cuser in self.mAllUsers.items():
                            if not (aconn == ccon):
                                if cuser.mIsConnected:
                                    # cuser.mSock.send(data.encode("utf-8"))
                                    cuser.mSock.sendall(data.encode("utf-8"))

                        auser.mContentQu.task_done()
                self.mLock.release()
            else:
                self.mLock.release()
            time.sleep(0.1)
  •  服务器运行起来。
  • 在我的电脑上运行客户端连接服务器
# coding=utf-8

import socket
import threading
import time
import sys


class CReceive(threading.Thread):

    def __init__(self, so):
        threading.Thread.__init__(self)
        self.mSock = so

    def run(self):

        while True:

            try:
                data = b''
                try:
                    while True:
                        MAX_LEN = 1024
                        m = self.mSock.recv(MAX_LEN)
                        if (m):
                            data += m
                            length = len(m)
                            if (length < MAX_LEN):
                                break
                        else:
                            break
                except Exception:
                    pass
                finally:
                    print("服务器发来:{}".format(data.decode("utf-8")))

            except Exception as e:
                print("error {}".format(e))
            else:
                pass


class CSend(threading.Thread):

    def __init__(self, so):
        threading.Thread.__init__(self)
        self.mSock = so

    def run(self):
        count = 0
        global gName
        while True:
            # line = input("输入:")
            line = input("我是用户{} {}输入:".format(gName, count))
            # line = "id{}: 我是用户{}没有啥话好说".format(gName, count)
            count += 1
            self.mSock.sendall(line.encode("utf-8") + b'suffix_end')
            time.sleep(0.2)


if __name__ == "__main__":
    length = len(sys.argv)
    print("length {}".format(length))
    gName = None
    if (length > 1):
        gName = sys.argv[1]

    print("start {}".format(gName))
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while True:
        result = sock.connect_ex(("3x.1xxx.179.xxx", 9090))
        # result = sock.connect_ex(("192.168.0.102", 9090))
        print("connect_ex {}".format(result))
        if (result == 0):
            rec = CReceive(sock)
            sen = CSend(sock)
            rec.start()
            sen.start()
            rec.join()
            sen.join()
            break
        else:
            time.sleep(4)

    print("主线程结束")

连接失败,返回10060

  • 登陆阿里云控制台,修改安全限制规则。云服务器ECS——>我的实例——>配置安全组规则——>配置规则

  • 手动添加服务器要用到的端口

  • 重启服务器
  • 再次测试。写的聊天室测试服务器能正常聊天了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值