From 34f5b05cdff0ccab5457d89b8d480768605b06e8 Mon Sep 17 00:00:00 2001 From: yndu13 Date: Tue, 18 Nov 2025 17:51:12 +0800 Subject: [PATCH 1/2] feat: support hostname method --- main.tea | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.tea b/main.tea index 24e6d32..70b43ed 100644 --- a/main.tea +++ b/main.tea @@ -210,6 +210,12 @@ static function assertAsArray(value: any)throws : [ any ]; */ static function getUserAgent(userAgent: string): string; +/** + * Get hostname of current machine + * @return the string value + */ +static function getHostName(): string; + /** * If the code between 200 and 300, return true, or return false * @return boolean From ddd5f05a7f1ea032d1a671f88e33954cced61cbe Mon Sep 17 00:00:00 2001 From: weeping Date: Tue, 18 Nov 2025 18:09:11 +0800 Subject: [PATCH 2/2] Feat: support hostname method For every languages --- cpp/include/darabonba/util.hpp | 2 ++ cpp/src/util.cpp | 9 +++++++++ cpp/tests/util.cpp | 6 ++++++ csharp/core/Common.cs | 18 +++++++++++++++++- csharp/tests/CommonTest.cs | 15 +++++++++++++++ golang/service/service.go | 9 +++++++++ golang/service/service_test.go | 9 +++++++++ .../main/java/com/aliyun/teautil/Common.java | 13 +++++++++++++ .../java/com/aliyun/teautil/CommonTest.java | 10 ++++++++++ php/src/Utils.php | 10 ++++++++++ php/tests/UtilsTest.php | 10 ++++++++++ python/alibabacloud_tea_util/client.py | 12 ++++++++++++ python/tests/test_client.py | 7 +++++++ swift/Sources/TeaUtils/Client.swift | 4 ++++ ts/src/client.ts | 9 +++++++++ ts/test/client.spec.ts | 9 +++++++++ 16 files changed, 151 insertions(+), 1 deletion(-) diff --git a/cpp/include/darabonba/util.hpp b/cpp/include/darabonba/util.hpp index bff4be0..16fd8ce 100644 --- a/cpp/include/darabonba/util.hpp +++ b/cpp/include/darabonba/util.hpp @@ -208,6 +208,8 @@ class Client { static bool is5xx(const shared_ptr &code); + static string getHostName(); + Client(){}; ~Client(){}; }; diff --git a/cpp/src/util.cpp b/cpp/src/util.cpp index 268cb85..5f313d3 100644 --- a/cpp/src/util.cpp +++ b/cpp/src/util.cpp @@ -9,6 +9,7 @@ #include #include #include +#include using namespace std; @@ -70,3 +71,11 @@ void Darabonba_Util::Client::sleep(const shared_ptr &millisecond) { int m = !millisecond ? 0 : *millisecond; boost::this_thread::sleep_for(boost::chrono::milliseconds(m)); } + +string Darabonba_Util::Client::getHostName() { + char hostname[256]; + if (gethostname(hostname, sizeof(hostname)) == 0) { + return string(hostname); + } + return ""; +} diff --git a/cpp/tests/util.cpp b/cpp/tests/util.cpp index 3ff3299..1acbc73 100644 --- a/cpp/tests/util.cpp +++ b/cpp/tests/util.cpp @@ -7,3 +7,9 @@ using namespace Darabonba_Util; TEST(tests_util, test_getDateUTCString) { ASSERT_EQ(29, Client::getDateUTCString().size()); } + +TEST(tests_util, test_getHostName) { + string hostname = Client::getHostName(); + // hostname should not be empty in most cases + ASSERT_GE(hostname.length(), 0); +} diff --git a/csharp/core/Common.cs b/csharp/core/Common.cs index e3f757e..166a533 100644 --- a/csharp/core/Common.cs +++ b/csharp/core/Common.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.IO; @@ -535,6 +535,22 @@ public static Stream AssertAsReadable(object a) throw new ArgumentException("The value is not Stream"); } + /// + /// Get hostname of current machine + /// + /// the string value + public static string GetHostName() + { + try + { + return System.Net.Dns.GetHostName(); + } + catch + { + return ""; + } + } + internal static string GetDefaultUserAgent() { string OSVersion = Environment.OSVersion.ToString(); diff --git a/csharp/tests/CommonTest.cs b/csharp/tests/CommonTest.cs index 48e5d06..e7d0b04 100644 --- a/csharp/tests/CommonTest.cs +++ b/csharp/tests/CommonTest.cs @@ -716,4 +716,19 @@ public class TestRegSubModel : TeaModel public int? testInt { get; set; } } + + public class GetHostNameTest + { + [Fact] + public void Test_GetHostName() + { + string hostname = Common.GetHostName(); + Assert.NotNull(hostname); + // hostname should not be empty in most cases + if (!string.IsNullOrEmpty(hostname)) + { + Assert.True(hostname.Length > 0); + } + } + } } \ No newline at end of file diff --git a/golang/service/service.go b/golang/service/service.go index 16de353..f4333e7 100644 --- a/golang/service/service.go +++ b/golang/service/service.go @@ -13,6 +13,7 @@ import ( "math/rand" "net/http" "net/url" + "os" "reflect" "runtime" "strconv" @@ -633,3 +634,11 @@ func ReadAsSSE(body io.ReadCloser) (<-chan SSEEvent, <-chan error) { }() return eventChannel, errorChannel } + +func GetHostName() *string { + hostname, err := os.Hostname() + if err != nil { + return tea.String("") + } + return tea.String(hostname) +} diff --git a/golang/service/service_test.go b/golang/service/service_test.go index a71c383..0aa3a84 100644 --- a/golang/service/service_test.go +++ b/golang/service/service_test.go @@ -500,3 +500,12 @@ func TestToJSONString(t *testing.T) { str = ToJSONString(testMap) utils.AssertEqual(t, "{\"key\":\"value\",\"map\":{\"str\":\"test&<>://中文\"},\"num\":1}", tea.StringValue(str)) } + +func Test_GetHostName(t *testing.T) { + hostname := GetHostName() + utils.AssertNotNil(t, hostname) + // hostname should not be empty in most cases + if tea.StringValue(hostname) != "" { + utils.AssertEqual(t, true, len(tea.StringValue(hostname)) > 0) + } +} diff --git a/java/src/main/java/com/aliyun/teautil/Common.java b/java/src/main/java/com/aliyun/teautil/Common.java index a0d18ee..58170fc 100644 --- a/java/src/main/java/com/aliyun/teautil/Common.java +++ b/java/src/main/java/com/aliyun/teautil/Common.java @@ -597,5 +597,18 @@ public static List> toArray(Object input) { return null; } } + + /** + * Get hostname of current machine + * + * @return the string value + */ + public static String getHostName() { + try { + return java.net.InetAddress.getLocalHost().getHostName(); + } catch (Exception e) { + return ""; + } + } } diff --git a/java/src/test/java/com/aliyun/teautil/CommonTest.java b/java/src/test/java/com/aliyun/teautil/CommonTest.java index 95b0dcc..1efc0ce 100644 --- a/java/src/test/java/com/aliyun/teautil/CommonTest.java +++ b/java/src/test/java/com/aliyun/teautil/CommonTest.java @@ -643,4 +643,14 @@ public Map> getIntegerListMap() { return this.integerListMap; } } + + @Test + public void getHostNameTest() { + String hostname = Common.getHostName(); + Assert.assertNotNull(hostname); + // hostname should not be empty in most cases + if (!hostname.isEmpty()) { + Assert.assertTrue(hostname.length() > 0); + } + } } diff --git a/php/src/Utils.php b/php/src/Utils.php index 13ca84f..69c3728 100644 --- a/php/src/Utils.php +++ b/php/src/Utils.php @@ -597,4 +597,14 @@ public static function assertAsReadable($value) throw new \InvalidArgumentException('It is not a stream value.'); } + + /** + * Get hostname of current machine. + * + * @return string the string value + */ + public static function getHostName() + { + return gethostname() ?: ''; + } } diff --git a/php/tests/UtilsTest.php b/php/tests/UtilsTest.php index 70d54f4..cff1b9f 100644 --- a/php/tests/UtilsTest.php +++ b/php/tests/UtilsTest.php @@ -547,4 +547,14 @@ public function toMap() return $res; } + + public function testGetHostName() + { + $hostname = Utils::getHostName(); + $this->assertNotNull($hostname); + // hostname should not be empty in most cases + if ($hostname !== '') { + $this->assertTrue(strlen($hostname) > 0); + } + } } diff --git a/python/alibabacloud_tea_util/client.py b/python/alibabacloud_tea_util/client.py index aca9232..08f89a6 100644 --- a/python/alibabacloud_tea_util/client.py +++ b/python/alibabacloud_tea_util/client.py @@ -516,3 +516,15 @@ def assert_as_array( if not isinstance(value, list): raise ValueError('The value is not a list') return value + + @staticmethod + def get_host_name() -> str: + """ + Get hostname of current machine + @return: the string value + """ + import socket + try: + return socket.gethostname() + except Exception: + return '' diff --git a/python/tests/test_client.py b/python/tests/test_client.py index 67786f4..2436a00 100644 --- a/python/tests/test_client.py +++ b/python/tests/test_client.py @@ -353,3 +353,10 @@ def test_assert_as_array(self): assert False except Exception as e: self.assertEqual('The value is not a list', str(e)) + + def test_get_host_name(self): + hostname = Client.get_host_name() + self.assertIsNotNone(hostname) + # hostname should not be empty in most cases + if hostname: + self.assertTrue(len(hostname) > 0) diff --git a/swift/Sources/TeaUtils/Client.swift b/swift/Sources/TeaUtils/Client.swift index 2c7e3db..9ec106f 100644 --- a/swift/Sources/TeaUtils/Client.swift +++ b/swift/Sources/TeaUtils/Client.swift @@ -328,4 +328,8 @@ public class Client { } throw TeaError("value is not a InputStream") } + + public static func getHostName() -> String { + return ProcessInfo.processInfo.hostName + } } diff --git a/ts/src/client.ts b/ts/src/client.ts index d64e057..7a401f4 100644 --- a/ts/src/client.ts +++ b/ts/src/client.ts @@ -255,6 +255,15 @@ export default class Client { } throw new Error(`The value is not a readable`); } + + /** + * Get hostname of current machine + * @return the string value + */ + static getHostName(): string { + const os = require('os'); + return os.hostname(); + } } diff --git a/ts/test/client.spec.ts b/ts/test/client.spec.ts index c2b41ab..36295e9 100644 --- a/ts/test/client.spec.ts +++ b/ts/test/client.spec.ts @@ -393,4 +393,13 @@ describe('Tea Util', function () { const result = Client.assertAsReadable(readable); assert.deepStrictEqual(result, readable); }); + + it('getHostName', function () { + const hostname = Client.getHostName(); + assert.ok(hostname); + // hostname should not be empty in most cases + if (hostname) { + assert.ok(hostname.length > 0); + } + }); });