linux多少个端口,Linux允许python使用多少个网络端口?
所以我一直在尝试在
python中多线程一些互联网连接.我一直在使用多处理模块,所以我可以绕过“Global Interpreter Lock”.但似乎系统只为python提供了一个开放的连接端口,或者至少它只允许一次连接发生.这是我所说的一个例子.
*请注意,这是在Linux服务器上运行
from multiprocessing import Process,Queue
import urllib
import random
# Generate 10,000 random urls to test and put them in the queue
queue = Queue()
for each in range(10000):
rand_num = random.randint(1000,10000)
url = ('http://www.' + str(rand_num) + '.com')
queue.put(url)
# Main funtion for checking to see if generated url is active
def check(q):
while True:
try:
url = q.get(False)
try:
request = urllib.urlopen(url)
del request
print url + ' is an active url!'
except:
print url + ' is not an active url!'
except:
if q.empty():
break
# Then start all the threads (50)
for thread in range(50):
task = Process(target=check,args=(queue,))
task.start()
因此,如果你运行它,你会注意到它在函数上启动了50个实例,但一次只运行一个.您可能认为“全球口译员锁”正在这样做但事实并非如此.尝试将函数更改为数学函数而不是网络请求,您将看到所有50个线程同时运行.
那么我必须使用套接字吗?或者我能做些什么可以让python访问更多端口?或者有什么我没有看到的?让我知道你的想法!谢谢!
*编辑
所以我编写了这个脚本来更好地使用请求库进行测试.好像我之前没有对它进行过这样的测试. (我主要使用urllib和urllib2)
from multiprocessing import Process,Queue
from threading import Thread
from Queue import Queue as Q
import requests
import time
# A main timestamp
main_time = time.time()
# Generate 100 urls to test and put them in the queue
queue = Queue()
for each in range(100):
url = ('http://www.' + str(each) + '.com')
queue.put(url)
# Timer queue
time_queue = Queue()
# Main funtion for checking to see if generated url is active
def check(q,t_q): # args are queue and time_queue
while True:
try:
url = q.get(False)
# Make a timestamp
t = time.time()
try:
request = requests.head(url,timeout=5)
t = time.time() - t
t_q.put(t)
del request
except:
t = time.time() - t
t_q.put(t)
except:
break
# Then start all the threads (20)
thread_list = []
for thread in range(20):
task = Process(target=check,time_queue))
task.start()
thread_list.append(task)
# Join all the threads so the main process don't quit
for each in thread_list:
each.join()
main_time_end = time.time()
# Put the timerQueue into a list to get the average
time_queue_list = []
while True:
try:
time_queue_list.append(time_queue.get(False))
except:
break
# Results of the time
average_response = sum(time_queue_list) / float(len(time_queue_list))
total_time = main_time_end - main_time
line = "Multiprocessing: Average response time: %s sec. -- Total time: %s sec." % (average_response,total_time)
print line
# A main timestamp
main_time = time.time()
# Generate 100 urls to test and put them in the queue
queue = Q()
for each in range(100):
url = ('http://www.' + str(each) + '.com')
queue.put(url)
# Timer queue
time_queue = Queue()
# Main funtion for checking to see if generated url is active
def check(q,timeout=5)
t = time.time() - t
t_q.put(t)
del request
except:
t = time.time() - t
t_q.put(t)
except:
break
# Then start all the threads (20)
thread_list = []
for thread in range(20):
task = Thread(target=check,time_queue))
task.start()
thread_list.append(task)
# Join all the threads so the main process don't quit
for each in thread_list:
each.join()
main_time_end = time.time()
# Put the timerQueue into a list to get the average
time_queue_list = []
while True:
try:
time_queue_list.append(time_queue.get(False))
except:
break
# Results of the time
average_response = sum(time_queue_list) / float(len(time_queue_list))
total_time = main_time_end - main_time
line = "Standard Threading: Average response time: %s sec. -- Total time: %s sec." % (average_response,total_time)
print line
# Do the same thing all over again but this time do each url at a time
# A main timestamp
main_time = time.time()
# Generate 100 urls and test them
timer_list = []
for each in range(100):
url = ('http://www.' + str(each) + '.com')
t = time.time()
try:
request = requests.head(url,timeout=5)
timer_list.append(time.time() - t)
except:
timer_list.append(time.time() - t)
main_time_end = time.time()
# Results of the time
average_response = sum(timer_list) / float(len(timer_list))
total_time = main_time_end - main_time
line = "Not using threads: Average response time: %s sec. -- Total time: %s sec." % (average_response,total_time)
print line
如您所见,它是多线程的.实际上,我的大部分测试表明,线程模块实际上比多处理模块更快. (我不明白为什么!)以下是我的一些结果.
Multiprocessing: Average response time: 2.40511314869 sec. -- Total time: 25.6876308918 sec.
Standard Threading: Average response time: 2.2179402256 sec. -- Total time: 24.2941861153 sec.
Not using threads: Average response time: 2.1740363431 sec. -- Total time: 217.404567957 sec.
这是在我的家庭网络上完成的,我服务器上的响应时间要快得多.我认为我的问题是间接回答的,因为我在一个更复杂的脚本上遇到了问题.所有的建议都帮助我很好地优化了它.谢谢大家!
linux多少个端口,Linux允许python使用多少个网络端口?相关推荐
- python 监控linux硬盘,Python3监控windows,linux系统的CPU、硬盘、内存使用率和各个端口的开启情况详细代码实例...
由于项目的需要,需要做一个简单监控服务器的CPU利用率.CPU负载.硬盘使用率.内存利用率和服务器的各个端口的开启情况的程序,并把结果通知到监控平台,如果出现异常,监控平台打电话或者发短信通知给具体的 ...
- 在Linux中进行docker网络端口映射
在Linux中有两种安装docker的方法,一种是使用官方安装脚本自动安装docker,另一种是手动安装docker. 容器中可以运行一些应用,要让外部也可以访问这些应用,可以通过"-P或- ...
- 什么是网络端口?或许工作10年的人也很难100%理解
你好,这里是网络技术联盟站. 今天和大家聊一个技术:网络端口. 我非常清晰的记得,当时上大学的时候学习网络这块,学到端口的时候就很难理解端口到底是个啥东西,因为我爱钻牛角尖,后来也搞明白了. 不过,我 ...
- 网络端口的作用及分类
一.什么是端口? 在开始讲什么是端口(port)之前,我们先来聊一聊什么是 port 呢?常常在网络上听说『我的主机开了多少的 port ,会不会被入侵呀!?』或者是说『开那个 port 会比较安全? ...
- linux 端口tnpl,利用Python找出9个连续的空闲端口
这篇文章主要介绍了Python找出9个连续的空闲端口的方法,感兴趣的小伙伴们可以参考一下 一.项目需求 安装某软件,配置时候需要填写空闲的端口.查看5个平台的某个端口是否被占用 5个平台为window ...
- Mac OS/Linux命令查询网络端口占用情况
2019独角兽企业重金招聘Python工程师标准>>> netstat命令 netstat -an | grep <port> <port> 为端口号 例子: ...
- Linux中不同进程同一个端口,linux系统实现多个进程监听同一个端口
通过 fork 创建子进程的方式可以实现父子进程监听相同的端口. 方法:在绑定端口号(bind函数)之后,监听端口号之前(listen函数),用fork()函数生成子进程,这样子进程就可以克隆父进程, ...
- linux删除80端口,linux下解决80端口被占用
安装一个nginx服务,在启动的时候报80端口被占用了,我们来检查一下有哪些服务占用了80端口 首先我们查一下占用80端口的有哪些服务,netstat -lnp|grep 80 查看80端口被那些服务 ...
- linux查看远程服务器端口,linux下查看本机和远程服务器的端口是否连通的方法...
linux下查看本机和远程服务器的端口是否连通的方法 如下所示: 1.ssh -v -p [端口号] [用户名]@[IP地址] 2.curl [IP地址]:[端口号] 以上这篇linux下查看本机和远 ...
最新文章
- 炼成优秀 SaaS 产品的三个要素?听腾讯、神策、网易的专家讲讲|PCon
- IBM AI辩手对战世界级人类辩手,炒作还是秀肌肉?
- jeewx-api-1.0.1(捷微微信接口API)版本正式发布
- VUEX封装module
- 指纹特征点提取代码matlab代码,科学网—MATLAB特征提取代码 - 蒋样明的博文
- 和菜鸟一起学linux之V4L2摄像头应用流程【转】
- dataframe 查找特定值_C++初级编程NOIP题:11H1544: 查找特定的值
- 全国计算机OFFICE二级考试大纲,全国计算机等级考试二级MSOffice高级应用考试大纲...
- matlab模糊pid控制教程,基于Matlab的自适应模糊PID控制器的设计
- 安卓手机网易云视频,下载的文件位置:
- 顺丰快递 : 请收下 MySQL 灵魂十连
- bean named 'transactionManager' available: No matching PlatformTransactionManager bean found for qua
- ECharts 使用xAxis.axisLine.lineStyle.color设置x坐标轴轴线颜色
- 计算机屏幕出现条纹w7,电脑屏幕出现条纹,教您电脑屏幕出现条纹怎么办
- mysql读写分离踩坑记
- Cased by: java.lang.ClassNotFoundException: com.google.common.util.concurrent.SettableFuture
- 杨辉三角形实现过程详解-C语言基础
- 新版本微信如何解绑手机号?
- Leetcode PHP题解--D70 784. Letter Case Permutation
- 仿腾讯手机管家火箭发射案例
热门文章
- qqkey获取原理_获取QQKEY源码[C++版]
- iview table 自定义列_案例 | iview中Table:拖拽适配列、自定义固定列、合并行
- python取出字符串中的偶数_从给定字符串中删除偶数个连续的重复字符
- C#总结项目《汽车租聘系统》项目代码实例【全注释版】
- 计算机二级html真题,计算机二级《Web程序设计》试题及答案
- php数组的值传递给另一个数组,如何把一个固定数组的值传递给另外一个数组
- python中字符移位加密_1.1 移位密码加密解密python实现
- 八、马科维茨投资组合
- 网页拉起QQ进行交谈
- 我与AI:我的“机器学习”过程