1、半开放socket
利用shutdown()函数使socket双向数据传输变为单向数据传输。shutdown()需要一个单独的参数,该参数表示了如何关闭socket。具体为:0表示禁止将来读;1
表示禁止将来写;2表示禁止将来读和写。
2、timeouts控制超时
调用socket的settimeout()函数,向其传递参数,表明超时时间设置。当访问一个socket,如果经过了参数设定的时间后,什么都没有发生,则会产生一个socket.timeout异常。
例如:当程序运行后,会等待数据传入。在另一终端,利用telnet连接12345端口。连接成功后,显示“连接来自:****”,如果此时5秒内,终端未进行输入,系
统会提示连接超时退出。
代码如下:
代码如下:
# -*- coding: cp936 -*-
##tcp响应服务器
import socket,traceback
host=''
port=12345
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host,port))
s.listen(1)
while 1:
try:
clientsock,clientaddr=s.accept()
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
continue
clientsock.settimeout(5)
try:
print "连接来自:",clientsock.getpeername()
while 1:
data=clientsock.recv(4096)
if not len(data):
break
clientsock.sendall(data)
clientsock.sendall("\nI get it!\n")
## t=raw_input('input the word:')
## clientsock.sendall(t)
except (KeyboardInterrupt,SystemExit):
raise
except socket.timeout:
print '连接超时'
pass
except:
traceback.print_exc()
try:
clientsock.close()
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
3、理解网络字节顺序
不同的平台有不同的二进制数据编码方法,为了解决这一总是,一种标准的二进制数据表示法,称为网络字节顺序。在发送一个二进制整数之前,该整数首先被
转换成网络字节顺序。接收方收到后,在使用该数据之前,会先把网络字节顺序转换成本地的表示方法。
python的struct模块提供了把数据在python和二进制数据之间转换的支持。
主要的两种基本格式:
H:适用于16位整数
I:适用于32位整数
惊叹号表示struct模块使用网络字节顺序来进行编码和解码。其它的格式见下表:
Character |
Byte order |
Size and alignment |
@ |
native |
native 凑够4个字节 |
= |
native |
standard 按原字节数 |
|
little-endian |
standard 按原字节数 |
> |
big-endian |
standard 按原字节数 |
! |
network (= big-endian) |
standard 按原字节数 |
常用语句:
struct.pack(fmt,v1,v2,...)将v1,v2按参数格式进行转换。参数fmt是格式字符
串,这里主要是!I。v1,v2,...表示要转换的python值。
struct.unpack(fmt,string)与pack刚好相反。
例如:
>>> import struct
>>> a=20
>>> str=struct.pack("!I",a)
>>> print repr(str)
'\x00\x00\x00\x14'
>>> print struct.unpack("!I",str)
(20,)

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to solve the problem of Jieba word segmentation in scenic spot comment analysis? When we are conducting scenic spot comments and analysis, we often use the jieba word segmentation tool to process the text...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.