如何将这段代码import re print("开始提取字符串") run = ".*" + start + "(.*)" + end + ".*" result = re.findall(run, str) for str_extraction in result: print(str_extraction)的str_extraction值得返回出来
时间: 2023-06-18 09:05:03 浏览: 40
你可以将`str_extraction`添加到一个列表中,并在函数末尾使用`return`语句将该列表返回。例如:
```python
import re
def extract_string(str, start, end):
print("开始提取字符串")
run = ".*" + start + "(.*)" + end + ".*"
result = re.findall(run, str)
extractions = []
for str_extraction in result:
extractions.append(str_extraction)
print(str_extraction)
return extractions
```
这样,当你调用`extract_string`函数时,它将返回一个包含所有提取的字符串的列表。你可以将该列表赋值给一个变量,然后在需要时使用。例如:
```python
my_str = "Hello World! This is a test string. Let's extract some text."
start = "This is "
end = " string."
extractions = extract_string(my_str, start, end)
print(extractions)
```
这将输出:
```
开始提取字符串
['a test']
```
相关问题
import time start_time = time.time() # 程序代码 end_time = time.time() run_time = end_time - start_time print("程序运行时间为:", run_time, "秒")在加一個結束時間
好的,你可以在程序结束前再加上一个结束时间的记录,然后计算程序的运行时间。下面是代码示例:
```python
import time
start_time = time.time()
# 程序代码
end_time = time.time()
run_time = end_time - start_time
print("程序运行时间为:", run_time, "秒")
```
你可以根据实际情况在程序代码段中添加你需要测试的部分。程序运行结束后,会输出程序的运行时间,单位为秒。
将下列代码改为python代码:import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.concurrent.CountDownLatch; import java.net.*; public class Scanport { public static void main(String[] args) throws InterruptedException { String host = "120.46.209.149"; int startPort = 1; int endPort = 1024; int start = 0; int end = 0; int step = Math.round(endPort / 1000) + 1;// 四舍五入函数 for (int i = 1; i < step; i++) { start = startPort + (i - 1) * 1000; end = startPort + i * 1000; System.out.println("正在扫描" + start + "-" + end); scan(host, start, end); } } public static void scan(String host, int startPort, int endPort) throws InterruptedException { List<Integer> portList = new ArrayList<>(); final CountDownLatch latch = new CountDownLatch(endPort - startPort + 1); for (int port = startPort; port <= endPort; port++) { int finalPort = port; Thread thread = new Thread(new Runnable() { @Override public void run() { try { Socket socket = new Socket(); socket.connect(new InetSocketAddress(host, finalPort), 100); System.out.println("TCP端口" + finalPort + "开放"); portList.add(finalPort); } catch (Exception e) { } try { DatagramSocket dsocket = new DatagramSocket(finalPort); dsocket.close(); } catch (SocketException e) { portList.add(finalPort); System.out.println("UDP端口" + finalPort + "开放"); } latch.countDown(); } }); thread.start(); } latch.await(); portList.sort(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }); } }
这是一个Java程序,需要转换为Python语言,转换后的代码如下:
```python
import threading
import socket
def scan(host, startPort, endPort):
portList = []
for port in range(startPort, endPort+1):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.1)
sock.connect((host, port))
print("TCP端口{}开放".format(port))
portList.append(port)
sock.close()
except:
pass
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(0.1)
sock.bind((host, port))
print("UDP端口{}开放".format(port))
portList.append(port)
sock.close()
except:
pass
portList.sort()
print(portList)
if __name__ == '__main__':
host = "120.46.209.149"
startPort = 1
endPort = 1024
step = (endPort // 1000) + 1
for i in range(1, step):
start = startPort + (i - 1) * 1000
end = startPort + i * 1000
print("正在扫描{}-{}".format(start, end))
scan(host, start, end)
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)