Python - access network

本文深入解析了TCP协议的工作原理,包括其如何在不可靠的IP层之上实现可靠的数据传输,通过重传丢失的数据包来确保数据的完整送达。此外,还介绍了TCP如何使用窗口机制进行流量控制,以及在网络编程中如何利用Python进行TCP套接字编程。

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

TCP Network

Transport Control Protocol(TCP) is built on top of IP.

TCP assumes IP might lose some data, so tcp will stores and retransmits data if it seems to be lost(when receive duplicate ACKs or ACK is time-out)

TCP will handle “flow control” using a transmit window (wnd)

TCP Connection / Sockets: network socket is an endpoint of a bidirectional inter-process communication flow.

TCP Port Number: a port is an application-specific software communications endpoint. It allows multiple networked applications to coexist on the same server.
Applications coexist at the same server
list of well-known port number


Sockets in Python

Python has built-in support for tcp sockets
import socket

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect('host', 'port number')
mysock.connect( ('www.py4inf.com', 80) )

# send our request, ask for some webpage
mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')

# receive data
while True:
    data = mysock.recv(512)
    if ( len(data) < 1 ) :
        break
    print data
# don't forget to close the socket
mysock.close()

we can make HTTP easier with urllib


using urllib model

urllib can do all the sockets work for us and make web pages look like a file. In a word, it is more convenient.

Note: there are some differences between python2 and python3 when using urllib. Here, I use python3 as standard.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值