python network programming tutorial

本文是一篇关于Python网络编程的教程,涵盖了客户端和服务器端的基本流程。客户端包括创建socket、连接服务器、发送和接收数据。服务器端涉及打开socket、绑定地址、监听连接以及使用多线程或select、poll方法来提高服务效率。文中还提到了如何使用select和poll进行更高效的通信,并给出了实际的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于网络编程以及socket 等一些概念和函数介绍就不再重复了,这里示例性用python 编写客户端和服务器端。


一、最简单的客户端流程:

1. Create a socket
2. Connect to remote server
3. Send some data
4. Receive a reply

 Python Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#Socket client example in python
 
import socket    #for sockets
import sys   #for exit
import struct
import time
 
#create an INET, STREAMing socket
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
     print  'Failed to create socket'
    sys.exit()
     
print  'Socket Created'
 
host =  'www.google.com';
port =  80;
 
try:
    remote_ip = socket.gethostbyname( host )
 
except socket.gaierror:
     #could not resolve
     print  'Hostname could not be resolved. Exiting'
    sys.exit()
 
#Connect to remote server
s.connect((remote_ip , port))
 
print  'Socket Connected to ' + host +  ' on ip ' + remote_ip
 
#Send some data to remote server
message =  "GET / HTTP/1.1\r\n\r\n"
 
try :
     #Set the whole string
    s.sendall(message)
except socket.error:
     #Send failed
     print  'Send failed'
    sys.exit()
 
print  'Message send successfully'
 
def recv_timeout(the_socket,timeout= 2):
     #make socket non blocking
    the_socket.setblocking( 0)
     
     #total data partwise in an array
    total_data=[];
    data= '';
     
     #beginning time
    begin=time.time()
     while  1:
         #if you got some data, then break after timeout
         if total_data  and time.time()-begin > timeout:
             break
         
         #if you got no data at all, wait a little longer, twice the timeout
         elif time.time()-begin > timeout* 2:
             break
         
         #recv something
         try:
            data = the_socket.recv( 8192)
             if data:
                total_data. append(data)
                 #change the beginning time for measurement
                begin=time.time()
             else:
                 #sleep for sometime to indicate a gap
                time.sleep( 0. 1)
         except:
             pass
"Python Network Programming Cookbook, 2nd Edition" ISBN: 1786463997 | 2017 | PDF | 442 pages | 15.41 MB Key Features Solve real-world tasks in the area of network programming, system/networking administration, network monitoring, and more Familiarize yourself with the fundamentals and functionalities of SDN Improve your skills to become the next-gen network engineer by learning the various facets of Python programming Book Description Python is an excellent language to use to write code and have fun by prototyping a pplications quickly. The presence of lots of third-party libraries, also known as batteries, makes it easier and faster to prototype an application or to implement a new algorithm. If you are interested in creating the building blocks for many practical web and networking applications that rely on networking protocols, then this book is a must-have. It employs Python for network programming to solve a variety of problems. This book highlights the major aspects of network programming in Python, starting from writing simple networking clients to developing complex Software-Defined Networking (SDN) systems and programming the Internet. It creates the building blocks for many practical web and networking applications that rely on various networking protocols. It presents the power and beauty of Python to solve numerous real-world tasks in the area of network programming, system and network administration, network monitoring, and web-application development. In this edition, you will also be introduced to network modelling to build your own cloud network. You will learn about the concepts and fundamentals of SDN and then extend your network with Mininet. Next, you'll find recipes on Authentication, Authorization, and Accounting and other alternative vendor-specific SDN approaches and frameworks. You will also learn to configure the Linux Foundation networking ecosystem and automate your networks with Python. By the end of this book, you will be able to analyze your network security vulnerabilities using advanced network packet capture and analysis techniques.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值