参考 微信公众号入门指引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -*- coding: utf-8 -*-
# filename: main.py
import web

urls = (
'/wx', 'Handle',
)

class Handle(object):
def GET(self):
return "hello, this is a test"

if __name__ == '__main__':
app = web.application(urls, globals())
app.run()
1
2
3
4
Traceback (most recent call last):
File "main.py", line 3, in <module>
import web
ImportError: No module named web
1
pip install web.py
1
2
3
4
5
6
7
8
9
10
11
12
Traceback (most recent call last):
File "main.py", line 15, in <module>
app.run()
File "/usr/lib/python2.7/site-packages/web/application.py", line 313, in run
return wsgi.runwsgi(self.wsgifunc(*middleware))
File "/usr/lib/python2.7/site-packages/web/wsgi.py", line 59, in runwsgi
return httpserver.runsimple(func, server_addr)
File "/usr/lib/python2.7/site-packages/web/httpserver.py", line 157, in runsimple
server.start()
File "/usr/lib/python2.7/site-packages/web/wsgiserver/__init__.py", line 1753, in start
raise socket.error(msg)
socket.error: No socket could be created

说明默认端口被占用,需要设置新的端口

1
python main.py 6789

正常启动服务

1
2
python main.py 0.0.0.0:6789
http://0.0.0.0:6789/

如果依然不能打开公网ip:6789/wx,可能是需要设置开放对应端口,以阿里云ECS为例

1
2
iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 6789 -j ACCEPT
service iptables save