python和lua的socket实例
发布日期:2021-06-30 19:39:15 浏览次数:2 分类:技术文章

本文共 1466 字,大约阅读时间需要 4 分钟。

python:

server.py

if __name__ == '__main__':    import socket    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    sock.bind(('localhost', 8001))    sock.listen(5)    while True:        connection,address = sock.accept()        try:            connection.settimeout(5)            buf = connection.recv(1024)            if buf == '1':                connection.send('welcome to server!')            else:                connection.send('please go out!')        except socket.timeout:            print 'time out'        connection.close()

client.py

if __name__ == '__main__':    import socket    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    sock.connect(('localhost', 8001))    import time    time.sleep(2)    sock.send('1')    print sock.recv(1024)    sock.close()
lua:

server.lua

socket = require("socket");host = host or "127.0.0.1";port = port or "8383";server = assert(socket.bind(host, port));ack = "ack\n";while 1 do    print("server: waiting for client connection...");    control = assert(server:accept());    while 1 do         command,status = control:receive();  if status == "closed" then break end        print(command);        control:send(ack);    endend
client.lua

local socket = require("socket")host = "127.0.0.1"port = 8383--打开一个TCP连接c = assert (socket.connect (host, port))c:send ("GET \n")while (true) do local s, status, partial = c:receive () print(s) if status == "closed" then break end c:send ("GET \n")endc:close ()

转载地址:https://linxinfa.blog.csdn.net/article/details/70228275 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:html学习笔记,好好学习,天天向上
下一篇:使用WinSCP从windows远程linux

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月18日 15时56分34秒