python可以开发我的世界服务端吗?

前几天突发奇想,想用python去开发一个我的世界服务器。

试着写了一点,看网上关于这部分的东西很少,于是决定发一个博客,把我遇到的一些问题分享给大家:

1.游戏与服务端通信使用的编码?

目前我找到的是cp437这个编码。

2.properties文件在python里读取方式?

我改进了一个大佬写的模块,可以用来读取properties后缀文件。

import re
import os
import tempfile
class Properties:
    def __init__(self, file_name):
        self.file_name = file_name
        self.properties = {}
        try:
            fopen = open(self.file_name, 'r')
            for line in fopen:
                line = line.strip()
                if line.find('=') > 0 and not line.startswith('#'):
                    strs = line.split('=')
                    self.properties[strs[0].strip()] = strs[1].strip()
        except Exception as e:
            raise e
        else:
            fopen.close()
    def has_key(self, key): 
        return key in self.properties 
    def get(self, key, default_value=''):
        if key in self.properties:
            return self.properties[key] 
        return default_value
    def put(self, key, value):
        self.properties[key] = value
        replace_property(self.file_name, key + '=.*', key + '=' + value, True)
def parse(file_name):
    return Properties(file_name)
def replace_property(file_name, from_regex, to_str, append_on_not_exists=True):
    file = tempfile.TemporaryFile()
    if os.path.exists(file_name):
        r_open = open(file_name,'r')
        pattern = re.compile(r'' + from_regex)
        found = None
        for line in r_open:
            if pattern.search(line) and not line.strip().startswith('#'):
                found = True
                line = re.sub(from_regex, to_str, line)
            file.write(line)
        if not found and append_on_not_exists:
            file.write('\n' + to_str)
        r_open.close()
        file.seek(0)
        content = file.read()
        if os.path.exists(file_name):
            os.remove(file_name)
        w_open = open(file_name,'w')
        w_open.write(content)
        w_open.close()
        file.close()
    else:
        pass

示例调用代码

[注:读取模块名称为property.py且在同级目录]

import property
import os
software_path = os.path.split(os.path.realpath(__file__))[0]  # 读取软件目录
config_mc_server_name="server.properties"
config_mc_server_path = os.path.join(software_path, config_mc_server_name)#读取配置文件位置
props = property.parse(config_mc_server_path)   #读取文件
motd=props.get('motd')#把server.properties的motd赋值到motd变量中

总结

总之感谢大家看完,因为是第一次写博客,所以难免有一些不当之处,以后我会继续改进!

这个服务端不知道能不能实现,如果有有经验的大佬可以提一些建议QwQ,感谢!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ERROR-403

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值