python 开启 socket 服务器

socket 服务器可通过 Python 来搭建。

 

1、安装 zope.interface

ope.interface 官网github

方式一:

~ $ pip install zope.interface
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Requirement already satisfied: zope.interface in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (4.1.1)
Requirement already satisfied: setuptools in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from zope.interface) (18.5)

这种方式下载的 zope.interface 没有作为 python 的组件。

 

方式二:

github 下载压缩包,下载好后点击打开,会看到文件夹下有个“setup.py”文件,右击->打开方式->BuildApplet,然后再在命令行下输入“python setup.py install”。这样就安装了 zope.interface

如果报错 [Errno 13] Permission denied: '/Library/Python/2.7/s,是权限问题,在命令前加上  sudo,相当于管理员操作了。

zope.interface-master $ sudo python setup.py install
Password:
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
  warnings.warn(msg)
running install
Checking .pth file support in /Library/Python/2.7/site-packages/
/usr/bin/python -E -c pass
TEST PASSED: /Library/Python/2.7/site-packages/ appears to support .pth files
running bdist_egg


Using /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Finished processing dependencies for zope.interface==4.6.1.dev0

 

 

2、安装 twisted

twisted 官网

方式一:

~ $ pip install Twisted
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting Twisted
  Downloading https://files.pythonhosted.org/packages/5d/0e/a72d85a55761c2c3ff1cb968143a2fd5f360220779ed90e0fadf4106d4f2/Twisted-18.9.0.tar.bz2 (3.1MB)
    100% |████████████████████████████████| 3.1MB 6.1MB/s 
Collecting zope.interface>=4.4.2 (from Twisted)
  Downloading https://files.pythonhosted.org/packages/28/39/0b5a168621fadd822810e8f3e2b9502b279aca07d6911ad06495ac9dff97/zope.interface-4.6.0-cp27-cp27m-macosx_10_6_intel.whl (140kB)
    100% |████████████████████████████████| 143kB 506kB/s 
Collecting constantly>=15.1 (from Twisted)
  Downloading https://files.pythonhosted.org/packages/b9/65/48c1909d0c0aeae6c10213340ce682db01b48ea900a7d9fce7a7910ff318/constantly-15.1.0-py2.py3-none-any.whl
Collecting incremental>=16.10.1 (from Twisted)

如果报错可试试用 pip3 命令。

~ $ pip3 install Twisted
Collecting Twisted
  Using cached https://files.pythonhosted.org/packages/5d/0e/a72d85a55761c2c3ff1cb968143a2fd5f360220779ed90e0fadf4106d4f2/Twisted-18.9.0.tar.bz2
Collecting zope.interface>=4.4.2 (from Twisted)
  Downloading https://files.pythonhosted.org/packages/14/22/ca298e4c576981830be5e706049e9a3c091edf9dc489a00e3c1ec8c501d3/zope.interface-4.6.0-cp37-cp37m-macosx_10_6_intel.whl (140kB)
    100% |████████████████████████████████| 143kB 249kB/s 
Collecting constantly>=15.1 (from Twisted)

这种方式下载的 Twisted 没有作为 python 的组件。

 

方式二:

zone.interface 的方式二相同。如果你的 MAC OS 版本高于 10.5,就下载 tarball 的那个版本。安装完成之后再用 “import twisted” 测试一下即可。

Twisted-18.9.0 $ sudo python setup.py install
no previously-included directories found matching '.travis'
no previously-included directories found matching 'tests'
warning: no previously-included files found matching 'examplesetup.py'
no previously-included directories found matching 'src/exampleproj'
no previously-included directories found matching 'src/incremental/newsfragments'

Installed /Users/cykj/Desktop/Twisted-18.9.0/.eggs/incremental-17.5.0-py2.7.egg
running install
Checking .pth file support in /Library/Python/2.7/site-packages/
/usr/bin/python -E -c pass
TEST PASSED: /Library/Python/2.7/site-packages/ appears to support .pth files
running bdist_egg


Installing easy_install script to /usr/local/bin
Installing easy_install-2.7 script to /usr/local/bin

Using /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Finished processing dependencies for Twisted==18.9.0

检查下 MAC 有没有 twisted

检查方法:打开 python 命令行,输入“import twisted”,如果没有任何提示,说明已经有 twisted 了,若提示没有模块,则需要安装 twisted

$ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Twisted
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named Twisted
>>> import twisted
>>> 

之后就可以用命令开启服务了。

 

3、开启 socket 服务

chatserver.py 下载地址。这里设置的端口号为 12345

from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor


class IphoneChat(Protocol):
	def connectionMade(self):
		#self.transport.write("""connected""")
		self.factory.clients.append(self)
		print "clients are ", self.factory.clients
	
	def connectionLost(self, reason):
	    self.factory.clients.remove(self)
	
	def dataReceived(self, data):
	    #print "data is ", data
		a = data.split(':')
		if len(a) > 1:
			command = a[0]
			content = a[1]
			
			msg = ""
			if command == "iam":
				self.name = content
				msg = self.name + " has joined"
				
			elif command == "msg":
				msg = self.name + ": " + content
			
			print msg
						
			for c in self.factory.clients:
				c.message(msg)
				
	def message(self, message):
		self.transport.write(message + '\n')


factory = Factory()
factory.protocol = IphoneChat
factory.clients = []

reactor.listenTCP(12345, factory)
print "Iphone Chat server started"
reactor.run()

终端 cd.py 文件目录,执行以下命令。

~ $ cd Desktop
Desktop $ python chatserver.py
Iphone Chat server started

看到提示:Iphone Chat server started 表示创建成功。

如果看到的提示是:No module named twisted.internet.protocol。表示本机上没有安装 twisted

You may also like...