服务器客户端通信Python

编程入门 行业动态 更新时间:2024-10-28 02:24:03
本文介绍了服务器客户端通信Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

服务器

import socket import sys s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) host= 'VAC01.VACLab' port=int(2000) s.bind((host,port)) s.listen(1) conn,addr =s.accept() data=s.recv(100000) s.close

客户

import socket import sys s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) host="VAC01.VACLab" port=int(2000) s.connect((host,port)) s.send(str.encode(sys.argv[1])) s.close()

我希望服务器接收客户端发送的数据.

I want the server to receive the data that client sends.

尝试此操作时出现以下错误

I get the following error when i try this

客户侧

回溯(最近通话最近): 在第21行的文件"Client.py"中 s.send(sys.argv [1]) TypeError:"str"不支持缓冲区接口

Traceback (most recent call last): File "Client.py", line 21, in s.send(sys.argv[1]) TypeError: 'str' does not support the buffer interface

服务器端

文件"Listener.py",第23行,在 数据= s.recv(100000) socket.error:[Errno 10057]不允许发送或接收数据的请求 因为未连接套接字,并且(当使用 sendto呼叫)未提供地址

File "Listener.py", line 23, in data=s.recv(100000) socket.error: [Errno 10057] A request to send or receive data was disallowed bec ause the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

推荐答案

在服务器中,您使用 listening 套接字接收数据.它仅用于接受新连接.

In the server, you use the listening socket to receive data. It is only used to accept new connections.

更改为此:

conn,addr =s.accept() data=conn.recv(100000) # Read from newly accepted socket conn.close() s.close()

更多推荐

服务器客户端通信Python

本文发布于:2023-08-07 03:30:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   通信   服务器   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!