• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python 网络编程起步(Socket发送消息)

Python 网络编程起步(Socket发送消息)

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python socket编程,python3 socket编程,python socket,python socket模块,python socketserver等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
一、服务端(Server.py)
    服务端要做的事情是:
    1. 创建一个Socket对象

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->import socket
s 
= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</div>    2. 绑定一个端口

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->s.bind(("", 8081))</div>    3. 接受来自客户端的消息

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->while True:
    
# Receive up to 1,024 bytes in a datagram
    data, addr = s.recvfrom(1024)
    
print "Received:", data, "from", addr</div>二、客户端(Client.py)
    客户端要做的事情是:
    1. 创建一个Socket对象。

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->import socket
s 
= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</div>    2. 向某个服务器的指定的端口发送消息。由于使用UDP,如果服务器端未接收到将会丢弃数据包。

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->port = 8081
host 
= "localhost"
while True:
    msg 
= raw_input()
    s.sendto(msg, (host, port))
</div>三、运行试试
</div>

您可能想查找下面的文章:

  • Python Socket编程详细介绍
  • Python Socket编程详细介绍
  • Python与Java间Socket通信实例代码
  • Python socket网络编程TCP/IP服务器与客户端通信
  • Python socket网络编程TCP/IP服务器与客户端通信
  • 结合Python的SimpleHTTPServer源码来解析socket通信
  • Python的Asyncore异步Socket模块及实现端口转发的例子
  • Python中基础的socket编程实战攻略
  • python实现简单socket通信的方法
  • python socket多线程通讯实例分析(聊天室)

相关文章

  • Python中的字典遍历备忘
  • Python实现八大排序算法
  • Phantomjs抓取渲染JS后的网页(Python代码)
  • python生成验证码图片代码分享
  • Python入门学习之字符串与比较运算符
  • python socket 超时设置 errno 10054
  • python实现的重启关机程序实例
  • Python构造函数及解构函数介绍
  • 基于python实现微信模板消息
  • python查找第k小元素代码分享

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • wxPython 入门教程
    • Python中处理字符串的相关的len()方法的使用简介
    • Python实现命令行通讯录实例教程
    • 在Python中的Django框架中进行字符串翻译
    • Python模拟登录验证码(代码简单)
    • Python多线程编程(二):启动线程的两种方法
    • Python去除字符串两端空格的方法
    • Python的Django框架中消息通知的计数器实现教程
    • Python深入学习之内存管理
    • python实现在字符串中查找子字符串的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有