python获取从命令行输入的数字代码演示#------------------------------------------------------------------------------# Name: numerical_input.py# Author: Kevin Harris# Last ……继续阅读 » 5年前 (2021-01-15) 1546浏览 488个赞
python使用utf-8编码编写代码,只需要在.py文件头部加上这句代码即可,加上后可以支持中文# -*- coding: utf-8 -*-……继续阅读 » 5年前 (2021-01-15) 1293浏览 2620个赞
python中字典(dict)和字符串之间的相互转换 字典(dict)转为字符串(string)我们可以比较容易的将字典(dict)类型转为字符串(string)类型。通过遍历dict中的所有元素就可以实现字典到字符串的转换:for key, value in sample_d……继续阅读 » 5年前 (2021-01-15) 2289浏览 442个赞
python中sleep函数用法演示,sleep用来暂停线程执行,单位为秒#------------------------------------------------------------------------------# Name: sleep.py# Author: Kevin Harris# ……继续阅读 » 5年前 (2021-01-15) 2864浏览 2606个赞
下面的python代码用于监控本机的8080端口,当用于通过http请求,服务器返回固定的html代码import SocketServerclass MyRequestHandler(SocketServer.BaseRequestHandler): def handle(self): print "From:&q……继续阅读 » 5年前 (2021-01-15) 1782浏览 821个赞
python通过urlunsplit函数将网址的多个部分合成一个完整的urlimport urlparseprint urlparse.urlunsplit(('http','www.sharejs.org:80', '/faq.cgi','src=fie','……继续阅读 » 5年前 (2021-01-15) 2476浏览 553个赞
python实现的多线程字符转换成大写的tcp服务端代码,下面的代码用于将用户通过tcp发过来的字符串转换成大写后返回,如果客户端发送过来空字符串,则结束通信import SocketServerimport netstringimport sysimport stringclass MyRequestHandler(SocketServer……继续阅读 » 5年前 (2021-01-15) 2171浏览 1781个赞
python连接url的不同部分,例如将http://www.75271.com/ 和 /other/path连接成一个完整的urlimport urlparseprint urlparse.urljoin('http://www.75271.com/', '/other/path')……继续阅读 » 5年前 (2021-01-15) 1613浏览 254个赞
python中迭代器(iterator)的用法#------------------------------------------------------------------------------# Name: iterators.py# Author: Kevin Harris# Last Modi……继续阅读 » 5年前 (2021-01-15) 1502浏览 2748个赞
python编写的简单的socket serverimport sockethost = ''port = 55555myServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)myServerSocket.setsockopt(socket.SOL……继续阅读 » 5年前 (2021-01-15) 2553浏览 367个赞
下面的python代码执行后通过tcp监控8081端口,用于将用户发送的请求字符串转换成大写后返回,如果用户发送的是end,则中断连接import SocketServerimport netstringclass MyRequestHandler(SocketServer.BaseRequestHandler): def handle(……继续阅读 » 5年前 (2021-01-15) 2375浏览 1871个赞
python执行get提交的操作import sys, urllib2, urllibdef addGETdata(url, data): """Adds data to url. Data should be a list or tuple consisting of 2-item lists o……继续阅读 » 5年前 (2021-01-15) 2357浏览 2266个赞
python打开url并按指定快大写读取网页内容import urllibpagehandler = urllib.urlopen("http://www.google.com")outputfile = open("index.html", "wb")while 1: data……继续阅读 » 5年前 (2021-01-15) 2855浏览 2364个赞
python对一个完整的url进行分割,将url分割成单独的部分,包括协议、域名、端口、路径、参数等等import urlparseprint urlparse.urlsplit('http://www.75271.com:80/faq.cgi?src=fie') ……继续阅读 » 5年前 (2021-01-15) 1622浏览 145个赞
python if else语句使用演示#------------------------------------------------------------------------------# Name: if_conditional.py# Author: Kevin Harris# Last Mo……继续阅读 » 5年前 (2021-01-15) 1390浏览 222个赞
下面的代码可以先创建一个目录,然后调用自定义的deleteDir函数删除整个目录#------------------------------------------------------------------------------# Name: create_directory.py# Author: K……继续阅读 » 5年前 (2021-01-15) 2898浏览 2226个赞
python自定义函数演示,计算Fibonacci数列#------------------------------------------------------------------------------# Name: function.py# Author: Kevin Harris# Last M……继续阅读 » 5年前 (2021-01-15) 2845浏览 1405个赞
python实现的一个简单的用于等待连接的socket服务器代码 import sockethost = '' port = 53333s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsocko……继续阅读 » 5年前 (2021-01-15) 1533浏览 2428个赞
通过tcp实现字符大写转换的python客户端代码,服务端代码参考:http://www.75271.com/codes/python/8081import socketimport netstrings = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect(('1……继续阅读 » 5年前 (2021-01-15) 1945浏览 2480个赞
python访问系统环境变量#------------------------------------------------------------------------------# Name: enviroment_variables.py# Author: Kevin Harris# Last Mo……继续阅读 » 5年前 (2021-01-15) 2580浏览 996个赞
python获得linux下的所有挂载点(mount points)# execute the external "mount" command and parse the output.import commands mount = commands.getoutput('mount -v')line……继续阅读 » 5年前 (2021-01-15) 2421浏览 1896个赞
python为socket handler创建一个处理类及调用方法import SocketServerport = 8000class myRequestHandler(SocketServer.StreamRequestHandler): def handle(self): print "connection ……继续阅读 » 5年前 (2021-01-15) 1947浏览 2196个赞
带错误处理的python socket server服务范例import socket, tracebackhost = ''port = 51423s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, s……继续阅读 » 5年前 (2021-01-15) 2562浏览 960个赞
python编写的一个简单的socket c/s用于发送和接受数据包 服务端代码import socketHOST = "127.0.0.1"PORT = 5000mySocket = socket.socket( socket.AF_INET, s……继续阅读 » 5年前 (2021-01-15) 2267浏览 2161个赞
python获得本地socket可用的设置信息import socketsolist = [x for x in dir(socket) if x.startswith('SO_')]solist.sort()for x in solist: print x ……继续阅读 » 5年前 (2021-01-15) 1968浏览 2036个赞
python实现绑定到特定地址和端口的socket serverimport socket, tracebackhost = '127.0.0.1'port = 51423s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SO……继续阅读 » 5年前 (2021-01-15) 1403浏览 1476个赞
python通过socket获得点对点的信息import socketprint "Creating socket...",s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print "done."print "Looking up ……继续阅读 » 5年前 (2021-01-15) 2068浏览 2904个赞
python通过socket远程连接错误处理代码演示import socket, syshost = sys.argv[1]textport = sys.argv[2]filename = sys.argv[3]try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)e……继续阅读 » 5年前 (2021-01-15) 3164浏览 796个赞
python字典遍历演示代码#the key and corresponding value can be retrieved at the same time using the #iteritems() method.knights = {'Key 1': 'value 1', 'key 2……继续阅读 » 5年前 (2021-01-15) 3505浏览 1740个赞
python清除字典内的全部数据(clear)d = {}d['name'] = 'Gumby'd['age'] = 42print dreturned_value = d.clear()print dprint returned_value……继续阅读 » 5年前 (2021-01-15) 2067浏览 1226个赞
python判断数组是否包含指定的元素的方法,直接使用in即可,python真是简单易懂print 3 in [1, 2, 3] # membership (1 means trueinventory = ["sword", "armor", "shield&qu……继续阅读 » 5年前 (2021-01-15) 2428浏览 2408个赞
python获取元素在数组中的索引号,python是通过index方法获取索引号的li = ['a', 'b', 'new', 'D', 'z', 'example', 'new', 'two'……继续阅读 » 5年前 (2021-01-15) 2616浏览 2133个赞
python查找两个字符串中相同的字符并输出seq1 = "spam" seq2 = "scam" res = [] for x in seq1: if x in seq2: ……继续阅读 » 5年前 (2021-01-15) 3354浏览 2255个赞
python获取数组元素的几种方法L = ['spam', 'Spam', 'SPAM!']print L[2] # offsets start at zeroprint L[-2] ……继续阅读 » 5年前 (2021-01-15) 2623浏览 380个赞
python字典复制演示代码L = [1,2,3]D = {'a':1, 'b':2}A = L[:] # instead of: A = L (or list(L))B = D.copy() # instead of: B = DA[1] = 'Ni……继续阅读 » 5年前 (2021-01-15) 3074浏览 932个赞
python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 5年前 (2021-01-15) 2859浏览 1374个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 5年前 (2021-01-15) 2827浏览 733个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 5年前 (2021-01-15) 4019浏览 534个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 5年前 (2021-01-15) 2501浏览 1046个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 5年前 (2021-01-15) 2974浏览 1271个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 5年前 (2021-01-15) 1991浏览 2967个赞
python检测是文件和是目录import os if os.path.isdir(path): print "it's a directory" elif os.path.isfile(path): print "it's a normal file" ……继续阅读 » 5年前 (2021-01-15) 2180浏览 1509个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 5年前 (2021-01-15) 2355浏览 1933个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 5年前 (2021-01-15) 3746浏览 2476个赞
python创建临时文件夹import tempfile, os tempfd, tempname = tempfile.mkstemp('.suffix') os.write(tempfd, "aString") # or, if you want a file-object: os.fdopen(te……继续阅读 » 5年前 (2021-01-15) 3082浏览 1375个赞