python搭建简单本地服务器
2016-02-01 00:17
661 查看
没搭建过服务器的人想搭建一个确实有点头疼,ME…
不过搭建出来后,如标题,没搭过的骚年继续,assert(reader instanceof 没服务器经验);
运行
**注意:**1.添加INTERNET权限;2.添加
不过搭建出来后,如标题,没搭过的骚年继续,assert(reader instanceof 没服务器经验);
环境
Ubuntu15.04Server
创建webserver3g.py
########################################################################### # Concurrent server - webserver3g.py # # # # Tested with Python 2.7.9 & Python 3.4 on Ubuntu 14.04 & Mac OS X # ########################################################################### import errno import os import signal import socket SERVER_ADDRESS = (HOST, PORT) = '', 8888 REQUEST_QUEUE_SIZE = 1024 def grim_reaper(signum, frame): while True: try: pid, status = os.waitpid( -1, # Wait for any child process os.WNOHANG # Do not block and return EWOULDBLOCK error ) except OSError: return if pid == 0: # no more zombies return def handle_request(client_connection): request = client_connection.recv(1024) print(request.decode()) http_response = b"""\ HTTP/1.1 200 OK Hello, World! """ client_connection.sendall(http_response) def serve_forever(): listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listen_socket.bind(SERVER_ADDRESS) listen_socket.listen(REQUEST_QUEUE_SIZE) print('Serving HTTP on port {port} ...'.format(port=PORT)) signal.signal(signal.SIGCHLD, grim_reaper) while True: try: client_connection, client_address = listen_socket.accept() except IOError as e: code, msg = e.args # restart 'accept' if it was interrupted if code == errno.EINTR: continue else: raise pid = os.fork() if pid == 0: # child listen_socket.close() # close child copy handle_request(client_connection) client_connection.close() os._exit(0) else: # parent client_connection.close() # close parent copy and loop over if __name__ == '__main__': serve_forever()
运行
$ python webserver3g.py
Client
private void httpGet() { String url = "http://192.168.0.101:8888/hello"; OkHttpUtils.get().url(url).build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e) { } @Override public void onResponse(String response) { Log.d(TAG, response); } }); }
**注意:**1.添加INTERNET权限;2.添加
compile 'com.zhy:okhttputils:2.2.0'作为网络请求;3.将192.168.0.101换成自己Server的IP;4.手机与Server在同一路由
参考
http://codingpy.com/article/build-a-simple-web-server-part-three/相关文章推荐
- python 百度top100和top500歌曲下载
- 采集百度top500歌曲,python2.7.2
- Python排序总结:列表、字典、包含字典的列表、包含元组的列表排序
- Python学习第七天
- python学习笔记之装饰器、递归、算法(第四天)
- python科学计算六:scipy矩阵操作
- The Python Tutorial
- Python 10.2 collections
- python笔记
- 《笨办法学Python》 第29课手记
- python Pillow 处理图片
- IronPython初试
- python在不同层级目录import模块的方法
- 在Python中移动目录结构的方法
- 学习Python语言---文件操作
- python嵌套函数使用外部函数变量的方法(Python2和Python3)
- Python函数式编程——map()、reduce()
- 学习Python语言---类和对象
- Python入门学习<一>
- LogisticRegression